puydan puydan - 1 month ago 21
JSON Question

How do I parse only value in json file

From this code. I want to parse only value from the json file

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.");

}
}


}

}


And the value is

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


I want to parse it after : symbol. (Is it value?) to show in richTextbox as Text.

Answer

Try this code

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

If you want only values joined by newline, then try this one

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