0

This question is probably asked a dozen times but i can't find anything useful so...

JSON data looks like this

{
"aaData": [
        [
        8120,
        "username",
        "[email protected]",
        "\/",
        "CUSTOMER ( SellerName )",
        "name",
        "<span class=\"label label-danger\">2015-08-05<\/span>",
        "<a class=\"btn btn-xs btn-primary manageDevices\" href=\"#\" id=\"manageDevices\" data-customerid=\"8120\" data-toggle=\"modal\">1<\/a>",
        "<a id=\"8120\" href=\"http:\/\/cms.*********.com:8081\/manageCustomers?customerId=8120\" class=\"btn btn-xs btn-primary\">View<\/a>",
        "YES"
         ],
         ....
         ]
     ],
    "sEcho": "NULL",
    "iTotalRecords": 65,
    "iTotalDisplayRecords": 65
}

Obviously, this fails:

private class OuterUser
{
    string id { get; set; }
    string username { get; set; }
    string line { get; set; }
    string reseller { get; set; }
    string username2 { get; set; }
    string date1 { get; set; }
    string manage { get; set; }
    string manageUser { get; set; }
    string active { get; set; }
}

dynamic jsonDe = JsonConvert.DeserializeObject<OuterUser>(rpl);
//dynamic j = JsonConvert.DeserializeObject<List<OuterUser>>(rpl);

Anyone has idea how to deserialize this? And explain what i did wrong. I don't need data on bottom (total records) and so on. Actually, only thing that i need is ID on beginning, username and email. Commented code also fails and i think that is part of solution.

2
  • Try JsonConvert.DeserializeObject<OuterUser[]>(rpl); as you wrote you need a array. Is there any container object? Commented Aug 10, 2015 at 11:58
  • @CSharper - i tried that also but i got this exception: An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code Also, i think that this "aaData" is some kind of container with rest of those users... Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'tvTracker.Data.IPTV+OuterUser[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. I need a list of those users, or array. Anything. Commented Aug 10, 2015 at 12:00

1 Answer 1

5

If you clean up your JSON (remove ....]) and post it to Json2Csharp.com, you'll get:

public class RootObject
{
    public List<List<object>> aaData { get; set; }
    public string sEcho { get; set; }
    public int iTotalRecords { get; set; }
    public int iTotalDisplayRecords { get; set; }
}

As the list of values you're interested in seems to be a nested array of various types. That can't be mapped to your OuterUser class, as the values aren't in key: value notation, they're just array elements.

So you'll have to deserialize to RootObject, then step into (or iterate over) the List<List<object>>:

var parsed = JsonConvert.DeserializeObject<RootObject>(rpl);
var firstUser = parsed.aaData[0]; 
var firstUserID = firstUser[0]; // 8120
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! Saved link also, seems pretty useful in my wars with JSON. Will accepts answer as soon as site allows it. Thanks!
It's also built into VS: Edit -> Paste Special -> Paste JSON As Classes. This'll give object[][] though, but you can iterate or index them just as easily.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.