2016-08-16 4 views
0

GitHub 레포에 저장된 C#을 사용하여 간단한 json 배열을 디코드하여 값이 포함되어 있는지 확인하고 싶습니다. Newtonsoft json 패키지를 사용하고 있습니다. 이 스레드를 읽었습니다 : Code for decoding/encoding a modified base64 URL,하지만 솔루션을 구현할 수없는 것 같습니다. 다음과 같은 오류가 발생합니다.URL에서 보낸 Base64 디코드 json 배열

System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters

그러나 나는 또한 코드에 어떤 변화가 있다고 생각합니다.

var value = "somestring"; 
var encodedTopicTypeURL ="https://api.github.com/repos/org/repo1/contents/sample.json"; 
string decodedTopicTypeURL; 
byte[] buffer = Convert.FromBase64String(encodedTopicTypeURL); 
decodedTopicTypeURL = Encoding.UTF8.GetString(buffer); 

using (var webClient = new System.Net.WebClient()) 
{ 
    var topicTypeJson = webClient.DownloadString(decodedTopicTypeURL); 
    JArray validTopicTypes = JArray.Parse(topicTypeJson); 
    if (!validTopicTypes.Contains(value)) 
    { 
     Logger.LogError($"Value not found"); 
    } 

JSON 배열 :

[ 
"string1", 
"string2", 
"string3", 
"string4",    
] 

답변

1

귀하의 질문은 당신이 자료-64/법에서 URL을 디코딩하려고하는 이유를 지정하지 않습니다 Ÿ encodedTopicTypeURL은 일반적인 문자열 일 뿐이며 아래처럼 직접 사용하지 않으시겠습니까? 당신이 실제로는 base64로 표현 떨어져 URL을 얻을 필요가있는 경우

var value = "somestring"; 
var url = "https://api.github.com/repos/org/repo1/contents/sample.json"; 

using (var webClient = new System.Net.WebClient()) 
{ 
    var topicTypeJson = webClient.DownloadString(url); 
    JArray validTopicTypes = JArray.Parse(topicTypeJson); 
    if (!validTopicTypes.Contains(value)) 
    { 
     Logger.LogError($"Value not found"); 
    } 
} 

, 다음 decodedTopicTypeURL 실제로 URL을해야합니다이

var encodedTopicTypeURL ="aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24="; 

이 방법을 다음 줄

var encodedTopicTypeURL ="https://api.github.com/repos/org/repo1/contents/sample.json"; 

교체 값.

당신은

1

https://api.github.com/repos/org/repo1/contents/sample.json 64 기수 문자열을 인코딩하지 않는다. 그것은 Base64로 표현 당신은 데이터를 다운로드 할 수 있습니다 당신이 그것을 문자열 변수에 (내가 검색 DownloadData(string url)을 사용 때 그것은하는 jobject에 구문 분석이 값 받아 내부 컨텐츠를 찾아 aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24=

입니다입니다 예를 들어

. :

static void Main(string[] args) 
    { 
     string url = @"https://api.github.com/"; 
     string searchString = "url"; 
     WebClient wc = new WebClient(); 
     wc.Headers.Add("user-agent", "Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 
     try 
     { 
      byte[] dat = wc.DownloadData(url); 
      string data = Encoding.ASCII.GetString(dat); 
      var values = JObject.Parse(data).Values(); 
      bool stringFound = values.Any(x => x.Path.Contains(searchString) || 
               x.Value<string>().Contains(searchString) 
             ); 
      if(stringFound) 
      { 
       Console.WriteLine("JSON contains search string"); 
      } 
     } 
     catch(Exception e) 
     { 
      throw; 
     } 

    } 

내가 할 수있는 값을 검색 확신 나은 (;

+0

예, 내가 GitHub의에서 JSON 파일의 URL에서 Base64로 표현을 참조 디코딩/Base64 인코딩 놀러 this 사이트를 사용할 수 있습니다. 문자열이 아니라고 말하면, 값을 확인하기 위해 키에 액세스하기 위해 JSON을 올바르게 디코딩하는 방법을 알고 싶습니다. 이 작업을 수행하는 방법을 보여주는 URL이 있거나 위에 나와있는 질문 및 코드를 기반으로하는 코드 샘플이있는 경우 – DBS