1

I have a json string like

{
    "action":"postRecord", 
     "data":{
            "0":{
                  "lid":999,
                   "cid":1234
                 },
            "1":{
                  "lid":111,
                  "cid":"6789"
                 }
        }
}

and

i want it co convert to Dictionary object so that i can get the data and iterate over it like data[0][lid] = 999 data[0][cid] =1234 data[1][lid] = 111 data[1][cid] = 6789

The problem is that i have to use ONLY Native Libraries of .net and I have version 2.0

4
  • read this one codeproject.com/Articles/156984/Serialize-JSON-Object-to-String Commented May 15, 2013 at 8:58
  • you will have to go for data[0]["cid"] / data[0]["lid"] as in other cases you will get undefined Error for cid/lid Commented May 15, 2013 at 9:03
  • Is there a reason you can't use outside libraries? Json.Net is the correct answer and there's a 2.0 version: james.newtonking.com/pages/json-net.aspx Commented May 15, 2013 at 9:24
  • The environment in which i want it to work does not allow Outside DLL may be because of Security Issues Commented May 15, 2013 at 11:21

2 Answers 2

3

You can do

Dictionary<string, object> obj = JsonConvert.DeserializeObject<Dictionary<string, object>>(Convert.ToString(data));

using JSON.NET

Sign up to request clarification or add additional context in comments.

1 Comment

I dont want to use any Third party dll. Only Native Libraries
1

If you can use .NET 3.5 (I was not sure whether your restriction to .NET 2.0 may be extended to 3.5) then this other question on the same topic has this great answer:

string json = @"{ ""id"": 13, ""value"": 255 }";

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

var dict = serializer.Deserialize<Dictionary<string, int>>(json);

You need to add a reference to System.Web.Extensions to use that class.

1 Comment

Thanks for the reply but I was stuck on 2.0 :( . I made a custom class to handle the situation though it take time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.