0

I'm trying to import JSON, parse it and be able to use it throughout my app. For example I would like to loop through and be able to print out the years. How would I go about doing that?

Code:

var result = await content.ReadAsStringAsync ();

try 
{
    List<RootObject> list = (List<RootObject>)JsonConvert.DeserializeObject<List<RootObject>>(result);

}

catch (Exception e) {
    Console.WriteLine (e);
}

JSON:

{
    "ROWCOUNT": 3,
    "COLUMNS": [
        "YEARMFG",
        "MAKE",
        "MODEL",
        "ENGINE",
        "TOWLIMIT",
        "NOTE1",
        "NOTE2"
    ],
    "DATA": {
        "YEARMFG": [
            2012,
            2012,
            2012
        ],
        "MAKE": [
            "Chevrolet/GMC",
            "Chevrolet/GMC",
            "Chevrolet/GMC"
        ],
        "MODEL": [
            "Avalanche 1500 4WD",
            "Avalanche 1500 4WD",
            "Avalanche 1500 4WD"
        ],
        "ENGINE": [
            "5.3L V-8",
            "5.3L V-8",
            "5.3L V-8"
        ],
        "TOWLIMIT": [
            5000,
            5500,
            8000
        ],
        "NOTE1": [
            "3.08 axle ratio",
            "3.42 axle ratio",
            "3.42 axle ratio"
        ],
        "NOTE2": [
            "",
            "",
            "Cooling or other accessory package required "
        ]
    }
}

Classes:

public class DATA
{
    public List<int> YEARMFG { get; set; }
    public List<string> MAKE { get; set; }
    public List<string> MODEL { get; set; }
    public List<string> ENGINE { get; set; }
    public List<int> TOWLIMIT { get; set; }
    public List<string> NOTE1 { get; set; }
    public List<string> NOTE2 { get; set; }
}

public class RootObject
{
    public int ROWCOUNT { get; set; }
    public List<string> COLUMNS { get; set; }
    public DATA DATA { get; set; }
}
Calum
  • 1,889
  • 2
  • 18
  • 36
Trey Copeland
  • 3,387
  • 7
  • 29
  • 46
  • 1
    What problem are you currently having? Is there an exception? – Corey Ogburn Feb 06 '15 at 20:01
  • What happens when you run the code you've given us? You appear to be on the right track, so its hard to tell what is currently tripping you up. – Tim Feb 06 '15 at 20:02
  • based on what he has shown it appears that he's trying to parse it and it doesn't look correct from first glance - http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c – MethodMan Feb 06 '15 at 20:02
  • I was told to add some classes from the JSON using json2csharp. My thought was I would be able to do list.YEARMFG or something like that. How do I access my values from json in the list? – Trey Copeland Feb 06 '15 at 20:04

2 Answers2

4

Assuming your objects and GetJson() just returns the JSON string:

var obj = JsonConvert.DeserializeObject<RootObject>(GetJSON());
foreach (var year in obj.DATA.YEARMFG)
{
    Console.WriteLine("Year: {0}", year);
}

Which should output (based on your sample data):

Year: 2012
Year: 2012
Year: 2012

Side note: You don't have to deserialize to a List<RootObject>. RootObject should be a single entity (as it wraps your entire JSON data). So simply deserialize it to a single instance, then access the DATA property and iterate over YEARMFG.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • Thanks Brad. This worked for me. I'm not sure if this is the semantic way of doing this, but it did work. Seems like I could just get away with having a DATA class instead of both DATA and RootObject. – Trey Copeland Feb 06 '15 at 20:18
  • You need `root` to decipher the `DATA` property within. You could just use `class RootObject { public DATA DATA { get; set; } }` if that's all you care about though. – Brad Christie Feb 06 '15 at 20:24
0

You are deserializing a List<RootObject>, so each element in the List is a RootObject. You can see in your class that RootObject has a DATA property, which contains a List<string> of the YEARMFG.

You should be able to access the items in your list like so:

list.DATA.YEARMFG will give you a list of the years for a given item.

If you would like to display them individually, you can loop through the list:

foreach (var yr in list.DATA.YEARMFG)
{
    // handle individual year info here
}
dub stylee
  • 3,252
  • 5
  • 38
  • 59
  • I thought that as well. But it only appears I can use `list` to Add, Count, Add Range, etc...normal Collections.List properties. – Trey Copeland Feb 06 '15 at 20:14