2

I tried using Newtonsoft.Json to deserialize this json string, but I'm not getting the desired output. my json string is

[
{
"id": 1,
"key": "Residential Homeowner",
"i18nText": "unknown message code DB_ENUM_UserType_residentialhomeowner",
"i18nKey": "DB_ENUM_UserType_residentialhomeowner"
},
{
"id": 8,
"key": "VAR Dealer \/ Builder",
"i18nText": "unknown message code DB_ENUM_UserType_vardealer\/builder",
"i18nKey": "DB_ENUM_UserType_vardealer\/builder"
},
{
"id": 2,
"key": "Administrator",
"i18nText": "unknown message code DB_ENUM_UserType_administrator",
"i18nKey": "DB_ENUM_UserType_administrator"
},
{
"id": 9998,
"key": "TempGuidUser",
"i18nText": "unknown message code DB_ENUM_UserType_tempguiduser",
"i18nKey": "DB_ENUM_UserType_tempguiduser"
},
{
"id": 9999,
"key": "GuidUser",
"i18nText": "unknown message code DB_ENUM_UserType_guiduser",
"i18nKey": "DB_ENUM_UserType_guiduser"
}
]

I just want the value of key when value of id=1. Generally json starts with {}(curly bracket) but here it is like [](square bracket). I've seen many examples but none worked for me.

Liam
  • 27,717
  • 28
  • 128
  • 190
Sumit Chourasia
  • 2,394
  • 7
  • 30
  • 57
  • possible duplicate of [Deserializing JSON Object Array with Json.net](http://stackoverflow.com/questions/18192357/deserializing-json-object-array-with-json-net) – James Nov 15 '13 at 13:46

4 Answers4

6

Generally json starts with {} (curly bracket), but here it is like [] (square bracket).

This is because you got an array of objects, not a single object. Arrays are serialized with square brackets around them. You should deserialize it into an array, and then grab the object at the index of interest.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • i tried using this class. but it is not working :( public class userType { public string id { get; set; } public string key { get; set; } public string i18nText { get; set; } public string i18nKey { get; set; } } – Sumit Chourasia Nov 15 '13 at 13:48
  • @SumitChourasia The class looks right. How did you call Newtonsoft's deserializer? Something like `JsonConvert.DeserializeObject`? – Sergey Kalinichenko Nov 15 '13 at 13:49
6

This is a related post that addresses JSON parsing in C#: C# JSON Parsing.

If the brackets are a problem, simply use:

string json = inputJson.Trim().Trim('[',']');

If the id can have a minimum value of 1, then this should work:

string GetKey(string inputJson)
{
    string key = inputJson.Substring(inputJson.IndexOf("key")+5);
    key = key.Substring(key.IndexOf("\""), key.IndexOf(",")-key.IndexOf("\""));
    key = key.Trim('\"');
    return key;
}
Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
4

If you are only interested in a single value from that larger JSON value, you may want to try Linq to JSON which would allow you to query over the JSON without deserializing everything.

Example:

JArray values = JArray.Parse(json);

string key;
var keyObject = values.FirstOrDefault(p => (int)p["id"] == 1);
if (keyObject != null)
{
     key = (string)keyObject["key"];
}
Sven Grosen
  • 5,616
  • 3
  • 30
  • 52
2

[] is to define a json object array. Your output should be an array. Traverse through the array like:

for(var i=0; i<output.Length; i++)
{
    if(output[i].id == "1") // desired id
    {
        Console.WriteLine(output[i].key);// use it as you wish
    }
}

and use the found objects key.

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
  • I just wanted to give you an idea of how you would approach the problem. @ledbutter's solution is of course the ideal one ;) – Tolga Evcimen Nov 15 '13 at 14:46