2016-10-25 10 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にTextとして表示するには、どうすればいいですか?

答えて

2

あなただけあなたがJson.Netを使用してモデルを作成することができ、この1

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

を試行し、改行が参加れる値たい場合

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 
    } 
} 

このコードを試してみてください。

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

保持するために2つの一時変数を導入し、キーと値

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をインストールします。名前空間を追加する

using Newtonsoft.Json.Linq; 

値を簡単に処理するクラスを作成します。

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(); 
     }