0

I am using Newtonsoft and Newtonsoft.Json. I have the below json:

string strJson_StorageInfo = "[{10:\"test\"}, {20:\"test1\"}]";

List<Dictionary<int, string>> jobj = (List<Dictionary<int, string>>) JsonConvert.DeserializeObject(strJson_StorageInfo, typeof(List<Dictionary<int, string>>));
foreach (Dictionary<int, string> dicStorageInfo in jobj) {
   foreach (KeyValuePair<int, string> StorageItem in dicStorageInfo) {
      Response.Write("key : " + StorageItem.Key + " , value : " + StorageItem.Value + "</br>"); 
   }
}

I need to Deserialize this. Can anyone suggest me good method. Thanks in advance

Bhavik
  • 4,836
  • 3
  • 30
  • 45
user1182553
  • 53
  • 2
  • 7
  • I only see a string here, no usage of Newtonsoft. Are you serializing the JSON above? – Mario Stoilov May 30 '14 at 10:27
  • List> jobj = (List>) JsonConvert.DeserializeObject(strJson_StorageInfo, typeof(List>)); foreach (Dictionary dicStorageInfo in jobj) { foreach (KeyValuePair StorageItem in dicStorageInfo) { Response.Write("key : " + StorageItem.Key + " , value : " + StorageItem.Value + ""); } } i feel unnecessary enumuration on list objects and then dictionary object. – user1182553 May 30 '14 at 10:36
  • http://stackoverflow.com/questions/14934360/how-to-deserialize-json-string-to-object-list-in-c-sharp-dot Did you try this? – chandresh patel May 30 '14 at 10:56

3 Answers3

0

you can use below mentioned code

string strJson_StorageInfo = "[{10:\"test\"}, {20:\"test1\"}]";
var abc = JsonConvert.DeserializeObject <List<Dictionary<int, string>>>(strJson_StorageInfo);

after you can find the keys and Values from it

  var K = abc.Select(p => p.Keys);
  var V = abc.Select(p => p.Values);
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
0

ok. I compromised with Json Format String. I have changed the json string. Found similar thread in How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?. Thanks.

Community
  • 1
  • 1
user1182553
  • 53
  • 2
  • 7
0

This worked for me.. I am not sure if it works in asp.net 2.0

string strJson_StorageInfo = "[{10:\"test\"}, {20:\"test1\"}]";
List<Dictionary<int, string>> values = JsonConvert.DeserializeObject<List<Dictionary<int, string>>>(strJson_StorageInfo);
foreach (var items in values)
{
    foreach (var item in items)
    {
        Label1.Text += "Key: " + item.Key.ToString() + " Value: " + item.Value.ToString() + "<br/>";
    }
}
Bhavik
  • 4,836
  • 3
  • 30
  • 45