-2

I have a json string like

{
  [
    "EnrityList": "Attribute",
    "KeyName": "AkeyName",
    "Value": "Avalue"
  ],
  [
    "EnrityList": "BusinessKey",
    "KeyName": "AkeyName",
    "Value": "Avalue"
  ]
}

I have serialized and got an object array. Could anyone help me to get the key value pair from these object array.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
S.Saranya
  • 11
  • 1
  • 1

3 Answers3

0

You can use JsonConvert from Newtonsoft.Json to deserialize json into Dictionary.

Dictionary<string, object> values = 
    JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonstring);
Vignesh
  • 1,824
  • 2
  • 10
  • 27
0

First convert the above string in proper json format by using :

str=str.Replace('[', '{');
str=str.Replace(']', '}');
//replace first occurance of {
int startPos = str.IndexOf('{');
str=str.Substring(0,startPos)+" ["+str.Substring(startPos + 1);
//replace last occurance of }
int endPos = str.LastIndexOf('}');
str=str.Substring(0,endPos)+"]";

This makes the string

str = [{"EnrityList":"Attribute","KeyName":"AkeyName","Value":"Avalue"}, {"EnrityList":"BusinessKey","KeyName":"AkeyName","Value":"Avalue"} ]   

Now, since you got the json string, you can easily work with it.
we can use method as given by

How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?

foreach(KeyValuePair<string, string> entry in myDictionary)
{
    // do something with entry.Value or entry.Key
}
Pradeep Singh
  • 432
  • 5
  • 11
-1

Looking at your example, You are trying to receive a List of certain type of elements, So First, You will need a class to represent your data type.

class MyType
{    
  string  EnrityList;
  string  KeyName;
  string  Value;    
}

Then use DesrializeObject method to store it in the variable

var values = JsonConvert.DeserializeObject<List<MyType>>(jsonstring);
Code Name Jack
  • 2,856
  • 24
  • 40
  • It will work for sure, We use many such things. Did you test? – Code Name Jack Dec 24 '18 at 08:25
  • `jsonstring` from question contains an invalid JSON. How can this work? – vasily.sib Dec 24 '18 at 08:26
  • THat's right, I have suggested an edit, op has messed up with the braces. – Code Name Jack Dec 24 '18 at 08:28
  • And even though, there is asked to get key-value pairs, not `List` – vasily.sib Dec 24 '18 at 08:31
  • I don't think there is a way to get a key-value pair out of it, Json Desrialization will only work with types that follow the same paradigm. This is a JSON array. It can be converted to KeyValuePair if had only two members (Key,Value). Otherwise you need to write your own type, or you need linq or a Conversion in loop. – Code Name Jack Dec 24 '18 at 08:34
  • 1
    Actualy, any object that is deserialized with `JsonConvert.DeserializeObject()` is a `JObject` at first. Which in turn is a `Dictionary`. For ex.: `{"a":1,"b":2,"c":3}` can be easily deserialized with `JsonConvert.DeserializeObject>()` without any custom behaviors. – vasily.sib Dec 24 '18 at 08:42