# Json.Net 5.0.8 and C#



## Emphyrio (May 16, 2009)

Hello 

I'm using Json.net 5.0.8 and VS 2010 .Net 4.0

My goal is to read some properties of a Json file into a string or array so I can use it in C#.

The json file looks like this (it's not the full list):


```
{
    "schema":5,
    "addons":[
        {
            "id":"{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}",
            "icons":{
                "32":"https://addons.cdn.mozilla.net/img/uploads/addon_icons/1/1865-32.png?modified=1372070145",
                "64":"https://addons.cdn.mozilla.net/img/uploads/addon_icons/1/1865-64.png?modified=1372070145"
            },
            "name":"Adblock Plus",
            "type":"extension",
            "version":"2.4.1",
         }
       ]
}
```
From this file I want to read the following properties:


id
name
type
version
For the sake of testing, I reduced it till "id"
I have this C# code:

```
private void test()
        {            
            string jPath="c:\\addons.json";
            string json;
            using (StreamReader r = new StreamReader(jPath))
            {
                json = r.ReadToEnd();
            }
                JObject ListAddons = JObject.Parse(json);
                string Addon = (string)ListAddons["schema"]["addons"][0]["id"];
                MessageBox.Show(Addon);
}
```
If I run to code I get a error message saying:


> System.InvalidOperationException was unhandled
> HResult=-2146233079
> Message=Cannot access child value on Newtonsoft.Json.Linq.JValue.


I can't figured it out why it can't access the Child[]?


----------



## Emphyrio (May 16, 2009)

I found a way 

This....


> string Addon = (string)ListAddons["schema"] ["addons"] [0]["id"] ;
> MessageBox.Show(Addon);


Has to be change to:


> Object Addon = ListAddons["addons"][0]["id"];
> MessageBox.Show(Addon.ToString());


So, that's solved, but it leaves me with a other question...

How do I get a list off all [addons] with the property ["id"] listed in the jason file ?


----------



## Emphyrio (May 16, 2009)

With trial-and-error I got this code:


```
private void GetPlug(string ffAddonPath)
            {
                // reading the json file
                // ffAddonPath is the path to the addons.json file
                
                string json;
                using (StreamReader r = new StreamReader(ffAddonPath))
                {
                    json = r.ReadToEnd();
                }
                var Items = JsonConvert.DeserializeObject<Target>(json);
                int countAddons = Items.addons.Count;
                lbPlugs.Items.Add("Addon count: " +countAddons );
                lbPlugs.Items.Add("");
                JObject o = JObject.Parse(json);
               
                for (int i = 0; i < countAddons; i++)
                {
                    string id = (string)o.SelectToken("addons["+i+"].id");
                    string name = (string)o.SelectToken("addons["+i+"].name");
                    string type = (string)o.SelectToken("addons["+i+"].type");
                    string version = (string)o.SelectToken("addons["+i+"].version");
                     // put it in a listbox to show on a form.                    
                    lbPlugs.Items.Add(id);
                    lbPlugs.Items.Add(name);
                    lbPlugs.Items.Add(type);
                    lbPlugs.Items.Add(version);
                    lbPlugs.Items.Add("-------------------------------------------------------");
                }
            }
```
I am sure there must be a better (logic) way to do this because it doesn't "feel" right.


----------



## Emphyrio (May 16, 2009)

Any suggestions anyone ?


----------

