1

I have some JSON with Two objects and these each have 3 objects nested.

{ "FirstPerson": { "number": "101", "a10": "1001", "a20": "1002" }, "SecondPerson": { "number": "102", "a10": "2001", "a20": "2001" } }

In c# asp.net mvc2 I've been able to get to "FirstPerson" or "SecondPerson" using a Hashtable but how do I get to "number" or "a10" when I know "FirstPerson"?

e.g. an objects inside an object.

Is a Hashtable the best use for this or should I be using something else?

Thanks in advance.

3 Answers 3

3

I found that solution for your problem may be give a clue to solve that

Want to convert a C# object into it's JSON equivalent? Here is a simple object from the System.Web.Script namespace that does exactly that:

System.Web.Script.Serialization.JavaScriptSerializer . It is stored in the System.Web.Extentions DLL (.Net Framework 3.5 only)

Using this object we serialize and deserialize objects in C#. Here is a quick sample:

A simple Employee object:

public class Employee
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string ID { get; set; }   
}

Adding some instances of them to a List:

Employee oEmployee1 =

   new Employee{Name="Pini",ID="111", Age="30"};

Employee oEmployee2 =

  new Employee { Name = "Yaniv", ID = "Cohen", Age = "31" };

Employee oEmployee3 =

    new Employee { Name = "Yoni", ID = "Biton", Age = "20" };

List oList = new List()

{ oEmployee1, oEmployee2, oEmployee3 };

Serializing then:

System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string sJSON = oSerializer.Serialize(oList);

And here is the output:

[{"Name":"Pini","Age":"30","ID":"111"},

{"Name":"Yaniv","Age":"31","ID":"Cohen"},

{"Name":"Yoni","Age":"20","ID":"Biton"}]

For your consideration here is the link http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx

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

2 Comments

This is very close but I want to Deserialize.
And the Deserialize method msdn.microsoft.com/en-us/library/bb355316.aspx isn't good enough... because ?
2

I have had good results using JsonConvert. It seems to do a good job of knowing what to do with collections. Just define the class you want to de-serialize to and have at it.

http://james.newtonking.com/projects/json-net.aspx

Example:

MyCollection col = JsonConvert.DeserializeObject<MyCollection>(this.HttpContext.Request.Params[0]);

Where MyCollection is a class which contains a collection of, in your case, people.

Comments

1

You could assign the JSON object to a dynamic variable and access the properties that way (only in C# 4.0 though)

dynamic jsonData = jsonObject;
int workflowNum = jsonData.SecondPerson[0].workflow;

2 Comments

Using DeserializeObject to get the jsonObject?
That or using JSON.NET to get the JSONObject and assign it to a dynamic variable. JSON.NET usage: blog.petegoo.com/archive/0001/01/01/… and JsonReader usage: nikhilk.net/CSharp-Dynamic-Programming-JSON.aspx

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.