29

I have a .NET project. I'm using the JSON.NET library. I need to use this library to parse some JSON. My JSON looks like this:

{"1":"Name 1","2":"Name 2"}

The object is really just a list of key/value pairs. I am trying to figure out how to use JSON.NET to 1) parse this JSON and 2) loop through the key/value pairs. Is there a way to do this? If so, how?

The only thing I see is de-serializing into a strongly-typed object.

Thank you so much!

5
  • Did you generate this JSON? Will it always be 1 and 2 as the keynames? Commented Apr 14, 2015 at 16:28
  • Well what have you tried so far in terms of JSON.NET? It looks like you should be able to parse this as a JObject, or convert it to a Dictionary<string, string> very easily... Commented Apr 14, 2015 at 16:28
  • possible duplicate of How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET? Commented Apr 14, 2015 at 16:28
  • Please post some code you've tried so we can help you further. Commented Apr 14, 2015 at 16:32
  • This question looks like the other side of this coin: stackoverflow.com/questions/29635808/… Commented Apr 14, 2015 at 19:46

3 Answers 3

75

You can deserialize to Dictionary<string, string>

var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
foreach(var kv in dict)
{
    Console.WriteLine(kv.Key + ":" + kv.Value);
}

Since JObject implements IDictionary, you can also simply use JObject.Parse

var dict = JObject.Parse(@"{""1"":""Name 1"",""2"":""Name 2""}");
Sign up to request clarification or add additional context in comments.

Comments

6

Below is the json Where subratings have key value pair

{  
"data": [    
{      
"id": "288560300",      
"lang": "en", 

"subratings":     {     

"Cleanliness": "5",

"Sleep Quality": "5",

"Service": "5"
  }
}
]}

public void LoadJsonKeyValuePair(string json)
{  
Rootobject item = JsonConvert.DeserializeObject<Rootobject>(json);
}

public class Rootobject
{
[JsonProperty("data")]
public List<Datum> data { get; set; }
}

public class Datum
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("lang")]
public string lang { get; set; }
[JsonProperty("subratings")]
public Dictionary<string, object> subratings { get; set; }
}

You can use Newtonsoft.Json to deserialize this object

2 Comments

Could you perhaps explain how to list the key/value pair with this example? I have a similar setup. Its not clear how to access the key/value pair with whats here.
is it possible to select only a few keys from the dictionary?
1

Hi all felt I should also share this

The above answer works for me using text box in asp.ne t

var jsonData = JsonConvert.DeserializeObject<Dictionary<string, string>>
(json_Incoming_fromServer);
foreach(var keyvalue in jsonData)
{

   textBox.text = keyvalue.Value; // this will only display the value of that
   // attribute / key 

}

thanks to EZI

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.