2

I'd like to convert a List<some_object> to JSON.

public class some_object
{
    public string field1 {get; set;}
    public string field2 {get; set;}
}

I want to serialize this:

var somejson = new {
    some_objects = new List<some_object>() {...some items...};
}

Standard serialization produces an array:

{
  "some_objects": [ 
    {
      "field1":"value1", 
      "field2":"value2"
    }, 
    {
      "field1":"value3", 
      "field2":"value4"
    } 
  ]
}

Instead, I want to produce something like this (more XML-like):

{
 "some_objects": 
  {
    "some_object": {"field1":"value1", "field2":"value2"},
    "some_object": {"field1":"value3", "field2":"value4"}
  }
}

Is there any way of producing this result?

3 Answers 3

3

No, JSON object properties need to be unique

All of the properties of your objects need to be different. Conceptually, XML represents an object, but the tags don't directly link to the concept of a property. The JSON way to do this would be what you posted:

{
  "some_objects": [ 
            {"field1":"value1", "field2":"value2"}, 
            {"field1":"value3", "field2":"value4"} 
  ]
}

Alternatively, use a dictionary giving items unique names

If you want to give each item a name that can then be used to access them, you can use a dictionary:

var dict = new { 
      some_objects = new Dictionary<string,some_object>(){ 
             { "a" , new some_object { field1="value1",field2="value2" } },
             { "b" , new some_object { field1="value3",field2="value4" } },
          } 
}

This would serialize into:

{
 "some_objects": 
  {
    "a": {"field1":"value1", "field2":"value2"},
    "b": {"field1":"value3", "field2":"value4"}
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

no, because you will get objects with the same name, called: some_object

think... if you could do it, how you will refer to one of them, using some_objects.some_object how do you know which is?

Comments

0

It seems like you want to store some kind of metadata, specifically the type of object, where with XML you could use an attribute. You would have to do something like add a separate property:

public class some_object
{
   public string typeText { get { return GetType().ToString(); } }

   public string field1 { get; set; }
   public string field2 { get; set; }
}

Which would theoretically generate JSON that looks something like:

{
  "some_objects": [ 
    {
      "typeText":"MyNamespace.some_object",
      "field1":"value1", 
      "field2":"value2"
    }, 
    {
      "typeText":"MyNamespace.some_object",
      "field1":"value3", 
      "field2":"value4"
    } 
  ]
}

Depending on your needs, you might want to encapsulate this metadata into its own class.

Comments

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.