2016-10-25 4 views
2

이 코드에서. 내가 JSON 파일json 파일의 값만 구문 분석하는 방법

if(openFileDialog1.ShowDialog() == DialogResult.OK) 
      {      
       using (StreamReader file = File.OpenText(openFileDialog1.FileName)) 
       using (JsonTextReader reader = new JsonTextReader(file)) 
       { 
        while (reader.Read()) 
        { 
         if (reader.Value != null) 
         { 
          richTextBox1.Text = reader.Value.ToString(); 
         } 
         else 
         { 
          MessageBox.Show("Error while parsing json file. Please try again."); 

         } 
        } 


       } 

      } 

에서만 값을 구문 분석 할 그리고 값이 난 후 그것을 분석 할

{ 
"install.and": "a", 
"install.emailAddress": "E-mailová adresa", 
"install.emailIncorrect": "Zadejte platnou e-mailovou adresu.", 
"install.emailRetryPrefix": "Neobdrželi jste e-mail? Zkuste to znovu", 
"install.emailRetry": "Zkuste to znovu", 
"install.emailSend": "Odeslat odkaz", 
"install.emailSent": "E-mail byl odeslán!", 
"install.emailSentTo": "E-mail byl odeslán", 
"install.emailText1": "Můžete navštívit", 
"install.emailText2": "Pokud nám poskytnete e-mailovou adresu, budeme vám moci poslat odkaz na pozdější instalaci.", 
"install.installing": "Instalace...", 
"install.later": "Instalovat později", 
"install.licenseAgreement": "licenční smlouva", 
"install.privacyPolicy": "zásady ochrany osobních údajů", 
"install.quit": "Ukončit instalační program" 
} 

입니다 : 기호. RichTextbox에 텍스트로 표시하려면 (값입니까?).

답변

2

만 줄 바꿈에 합류 값들하려면이 코드

using (StreamReader file = File.OpenText(openFileDialog1.FileName)) 
using (JsonTextReader reader = new JsonTextReader(file)) 
{ 
    var o = JObject.Load(reader); 
    foreach (var v in o) 
    { 
     var value = v.Value.Value<string>(); 
     //do whatever you want with value 
    } 
} 

을 시도,이 하나의

using (StreamReader file = File.OpenText(openFileDialog1.FileName)) 
using (JsonTextReader reader = new JsonTextReader(file)) 
{ 
    var o = JObject.Load(reader); 
    var e = o.Values().Select(x => x.Value<string>()); 
    var values = string.Join(Environment.NewLine, e); 

    //do whatever you want with values 
} 
+0

매우 유용합니다 ~~ 감사합니다. D – puydan

0

당신은 Json.Net를 사용하여 시도하고 모델 생성 :

public class JsonObject 
{ 
    [JsonProperty("install.and")] 
    public string install_and { get; set; } 
    [JsonProperty("install.emailAddress")] 
    public string emailAddress { get; set; } 
    [JsonProperty("install.emailIncorrect")] 
    public string emailIncorrect { get; set; } 
    [JsonProperty("emailRetryPrefix")] 
    public string emailRetryPrefix { get; set; } 
    [JsonProperty("install.emailRetry")] 
    public string emailRetry { get; set; } 
    [JsonProperty("install.emailSend")] 
    public string emailSend { get; set; } 
    [JsonProperty("install.emailSent")] 
    public string emailSent { get; set; } 
    [JsonProperty("install.emailSentTo")] 
    public string emailSentTo { get; set; } 
    [JsonProperty("install.emailText1")] 
    public string emailText1 { get; set; } 
    [JsonProperty("install.emailText2")] 
    public string emailText2 { get; set; } 
    [JsonProperty("install.installing")] 
    public string installing { get; set; } 
    [JsonProperty("install.later")] 
    public string later { get; set; } 
    [JsonProperty("install.licenseAgreement")] 
    public string licenseAgreement { get; set; } 
    [JsonProperty("install.privacyPolicy")] 
    public string privacyPolicy { get; set; } 
    [JsonProperty("install.quit")] 
    public string quit { get; set; } 
} 

그러면 json 파일을 prase 할 수 있습니다.

string json_data = "{\"install.and\": \"a\",\"install.emailAddress\": \"E-mailová adresa\",\"install.emailIncorrect\": \"Zadejte platnou e-mailovou adresu.\",\"install.emailRetryPrefix\": \"Neobdrželi jste e-mail? Zkuste to znovu\",\"install.emailRetry\": \"Zkuste to znovu\",\"install.emailSend\": \"Odeslat odkaz\",\"install.emailSent\": \"E-mail byl odeslán!\",\"install.emailSentTo\": \"E-mail byl odeslán\",\"install.emailText1\": \"Můžete navštívit\",\"install.emailText2\": \"Pokud nám poskytnete e-mailovou adresu, budeme vám moci poslat odkaz na pozdější instalaci.\",\"install.installing\": \"Instalace...\",\"install.later\": \"Instalovat později\",\"install.licenseAgreement\": \"licenční smlouva\",\"install.privacyPolicy\": \"zásady ochrany osobních údajů\",\"install.quit\": \"Ukončit instalační program\""; 

JsonObject data = JsonConvert.DeserializeObject<JsonObject>(json_data); 
richTextBox1.Text = data.emailAddress; 
richTextBox2.Text = data.emailIncorrect; 
richTextBox3.Text = data.emailRetry; 
[...] 
1

유지하기 위해 두 개의 임시 변수를 소개 키와 값

string key = string.Empty; 
    string value = string.Empty; 

수정이처럼 while 루프,

using (JsonTextReader reader = new JsonTextReader(file)) 
      { 
       while (reader.Read()) 
       { 
        if (reader.Value != null) 
        { 
         key = reader.Value.ToString(); 
         if (reader.Read()) 
          value = reader.Value.ToString(); 
         Console.WriteLine("{0} : {1}", key,value); 
         //Instead of writing in a console, process and write it in Rich text box. 
        }       
       } 
      } 
0

첫째, nuget 패키지 관리자에서 newtonsoft.json를 설치합니다. 네임 스페이스 추가

쉽게 값을 처리 할 수있는 클래스를 만듭니다.

class Details 
{ 
    public string and; 
    public string EmailAddress; 
    public string EmailIncorrect; 
    public string EmailRetry; 
    public string EmailSend; 
    public string EmailSent; 

} 

그런 다음 파일을 읽은 다음 JObject를 사용하여 구문 분석하십시오.

if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      string file = File.ReadAllText(openFileDialog1.FileName); 
      JObject jo = JObject.Parse(file); 
      Details dt = new Details(); 
      dt.and = (string)jo["install.and"]; 
      richTextBox1.Text = reader.Value.ToString(); 
     } 
관련 문제