3

I have to deserialize the following json response (the Result list has variable length):

{
    "ResultSet": {
        "Query": "volkswagen",
        "Result": [
            {
                "symbol": "VLKAY",
                "name": "Volkswagen AG",
                "exch": "PNK",
                "type": "S",
                "exchDisp": "OTC Markets",
                "typeDisp": "Equity"
            },
            {
                "symbol": "VOW3.DE",
                "name": "Volkswagen AG",
                "exch": "GER",
                "type": "S",
                "exchDisp": "XETRA",
                "typeDisp": "Equity"
            },
            {
                "symbol": "VOW.DE",
                "name": "Volkswagen AG",
                "exch": "GER",
                "type": "S",
                "exchDisp": "XETRA",
                "typeDisp": "Equity"
            }
        ]
    }
}

What I got:

JavaScriptSerializer js = new JavaScriptSerializer();
string jsonString = "...String is here...";
SearchObj obj = js.Deserialize<SearchObj>(jsonString);

I understand that I usually have to create a fitting obj. e.g. SearchObj which will get filled but in this case I'm not entirely sure how this object is supposed to look like. I came up with:

class Data 
{
    public string symbol { get; set; }
    public string name { get; set; }
    public string exch { get; set; }
    public string type { get; set; }
    public string exchDisp { get; set; }
    public string typeDisp { get; set; }
}

class Container 
{
    public string Query { get; set; }
    public List<Data> Result { get; set; }
}

class SearchObj
{
    public Container ResultSet { get; set; }
}

But guess what, it's not working, I only get ResultSet = null.

DHN
  • 4,807
  • 3
  • 31
  • 45
  • Do you get a parsing error/exception? – phoenix7360 Mar 18 '13 at 12:06
  • Why not have a look at dynamics, http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – Dutts Mar 18 '13 at 12:08
  • @phoenix7360 No errors/exceptions. Richard I will look into it. –  Mar 18 '13 at 12:10
  • Does the object not get filled? Do some of the properties get filled? – rhughes Mar 18 '13 at 12:10
  • @rhughes SearchObj.ResultSet = null -> can't check for any other values. I already did some string fiddling in order to make it easier to parse, since I only need ´Result´ but I would rather like to learn to parse this as well. –  Mar 18 '13 at 12:14
  • I did ask a similar question. Maybe this can help you -> http://stackoverflow.com/a/14903514/147453 – tucaz Mar 18 '13 at 12:14
  • 1
    I tested your code and it works: resultset=3 – giammin Mar 18 '13 at 12:20

3 Answers3

1

Try to change your class Container as

 class Container 
 {
     public string Query { get; set; }
     public Data[] Result { get; set; }
 } 

I have not tested it, based on my observation

Satpal
  • 132,252
  • 13
  • 159
  • 168
1

I always feel bad when I answer my own question but here it goes.

Basically my idea was correct, I only made one mistake which is that I don't need the

class SearchObj
{
    public Container ResultSet { get; set; }
}

Using

Container obj = js.Deserialize<Container>(jsonString);

instead of

SearchObj obj = js.Deserialize<SearchObj>(jsonString);

made the trick. Both Data[] and List<Data> in Container work btw.

Edit: From giammins comment it seems that it is working on some machines without that change but I guess that's a case for undefined behavior.

0

You can use http://www.json2charp.com to create your classes.

Philippe Maes
  • 510
  • 9
  • 26