0

I want to create a multidimensional json object based on a c# class. I normally do it like this:

public class foo
{
    public string name { get; set; }
    public int age { get; set; }
}

And serialize a new instance of the class with a JavaScriptSerializer. But lets say that i want to add another json array containing the persons friends inside the main json array. Example array: Accessing data in a multidimensional JSON array with jQuery

Hope you get the idea. Thanks

1
  • What you asking for isn't an array. It is a hash objects with properties named "0", "1", etc. If that is what you want, then that is how you should define your foo class. Commented Oct 17, 2011 at 14:46

2 Answers 2

1

If I get your question corect, something like this should work...

public class Person 
{     
  public string name { get; set; }     
  public int age { get; set; } 
  public List<Person> Friends { get; set; }
} 
Sign up to request clarification or add additional context in comments.

Comments

0

How are you creating the JSON version of your class, foo?

JavaScriptSerializer and Controller.JSON (in MVC) support serializing IEnumerable derived types, so you could have the class:

public class foo
{
    public string name { get; set; }
    public int age { get; set; }
    public List<Bar> friends {get; set;}
}

and your JSON serialized class would include the list of Bar objects.

1 Comment

Im just creating a new instance of the class, adding some values, and go something like new JavaScriptSerializer.serialize(object). So you got my point and your answer is correct as well. Thanks for answering but ill accept YWes since he posted first :)

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.