我如何解析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"
}

我想在:symbol之后解析它。(是值吗?)显示在richTextbox作为文本。

试试这个代码

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
}

引入两个临时变量来保存键和值

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

可以使用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文件:

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;
[...]

首先,安装newtonsoft。从nuget包管理器中获取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();
        }

相关内容

  • 没有找到相关文章

最新更新