28

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!

JQuery Mobile
  • 6,221
  • 24
  • 81
  • 134
  • Did you generate this JSON? Will it always be 1 and 2 as the keynames? – mason Apr 14 '15 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` very easily... – Jon Skeet Apr 14 '15 at 16:28
  • possible duplicate of [How can I deserialize JSON to a simple Dictionary in ASP.NET?](http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – Jamie Dixon Apr 14 '15 at 16:28
  • Please post some code you've tried so we can help you further. – Biscuits Apr 14 '15 at 16:32
  • This question looks like the other side of this coin: http://stackoverflow.com/questions/29635808/serialize-objects-as-json#29635808 – Vaccano Apr 14 '15 at 19:46

3 Answers3

70

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""}");
EZI
  • 15,209
  • 2
  • 27
  • 33
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

  • 1
    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. – Autonomic Jul 08 '17 at 14:25
  • is it possible to select only a few keys from the dictionary? – mrlucasrib May 09 '21 at 10:50
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

Waheed Rafiq
  • 460
  • 1
  • 6
  • 17