0

I have string json as

"[{\"Key\":\"a\",\"Value\":\"1\"},{\"Key\":\"b\",\"Value\":\"2\"}}]"

I want parse to object

class abc{
public string a{get; set;}
public string b{get; set;}
}

Please, help me

  • 1
    do you get some error, or something? I hope you tried to find solution by yourself, right :) http://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object – Arkadiusz Raszeja May 18 '17 at 08:17
  • Why are 2K+ rep users answering this obvious duplicate? Read [answer]. – CodeCaster May 18 '17 at 08:19
  • No, json string as key-value pair. I want convert key-value pair to object class – Thoại Nguyễn May 18 '17 at 08:20
  • @CodeCaster: That isn't quite an exact dupe - there may be one out there that is but in this case the complexity is that rather than having json like `{Propety: Value}` it is `{Key: "propertyname", Value: "Value"}` which is a harder question. – Chris May 18 '17 at 08:22

3 Answers3

1

you have to Newtonsoft Library and use

var dict=  JsonConvert.DeserializeObject<Dictionary<string,int>>("[{\"Key\":\"a\",\"Value\":\"1\"},{\"Key\":\"b\",\"Value\":\"2\"}}]"); 
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Int32]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. – Thoại Nguyễn May 18 '17 at 08:33
0

Json.NET is a Popular high-performance JSON framework for .NET

Install this nuget package then it's as simple as:

var json = "[{\"Key\":\"a\",\"Value\":\"1\"},{\"Key\":\"b\",\"Value\":\"2\"}}]";

var items = JsonConvert.DeserializeObject<List<abc>>(json);
Neil
  • 11,059
  • 3
  • 31
  • 56
0

I don't know about parsing to a defined object. However you can use A .Net library called Newtonsoft JObject o = JObject.Parse(x.ToString());

With JObject being an instance of Newtonsoft.JSON.Linq.JObject

Elie
  • 321
  • 3
  • 11