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

4
  • 1
    do you get some error, or something? I hope you tried to find solution by yourself, right :) stackoverflow.com/questions/4611031/… Commented May 18, 2017 at 8:17
  • Why are 2K+ rep users answering this obvious duplicate? Read How to Answer. Commented May 18, 2017 at 8:19
  • No, json string as key-value pair. I want convert key-value pair to object class Commented May 18, 2017 at 8: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. Commented May 18, 2017 at 8:22

3 Answers 3

1

you have to Newtonsoft Library and use

var dict=  JsonConvert.DeserializeObject<Dictionary<string,int>>("[{\"Key\":\"a\",\"Value\":\"1\"},{\"Key\":\"b\",\"Value\":\"2\"}}]"); 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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);

2 Comments

Will that actually cope with the format being Key/value attributes rather than the more usual {a: Value} type syntax? If I run the code I get null for a and b...
Yes. This' s format key/value
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.