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.

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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; } }
@SumitChourasia The class looks right. How did you call Newtonsoft's deserializer? Something like JsonConvert.DeserializeObject<MyClass[]>?
5

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"];
}

Comments

5

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;
}

3 Comments

I just want the value of key when value of id=1.
This is too much complicated. :(
Thanks for your effort. but implementation with linq is very simple :)
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.

1 Comment

I just wanted to give you an idea of how you would approach the problem. @ledbutter's solution is of course the ideal one ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.