0

I need my JSON output to be like the following (array of object}:

{
    "values":[{"1","one"},{"2","two"},{"3","three"},{"4","four"}]
}

However, when I serialize the following C# class:

public class MyObject
{
    public List<string>[] values {get;set}
}

It results in the following (array of array):

{
    "values":[["1","one"],["2","two"],["3","three"],["4","four"]]
}

I've tried many variations on this object. Like the following:

public class MyObject
{
    public KeyValuePair[] values {get;set}
}

Which gives me (array of KeyValuePair):

{
    "values":[{"Key":"1","Value":"one"},{"Key":"2","Value":"two"},{"Key":"3","Value":"three"},{"Key":"4","Value":"four"}]
}

Is there a C# object property which would serialize to an array of json objects which do not have the object property names included?:

{
    "values":[{"1","one"},{"2","two"},{"3","three"},{"4","four"}]
}
1
  • 5
    That JSON is invalid, because that you cannot create it. two brakets mean an object, and an object must have named properties, not elements like an array, maybe you want "values":[["1","one"],["2","two"],["3","three"],["4","four"]? Commented Apr 10, 2015 at 17:22

1 Answer 1

1

I may be answering my own question here, but here is what I found. The following will return an array of objects without property names. However, it is a hack and uses the Dictionary Key as the json property name:

public class MyObject
{
    public Dictionary<string, string>[] values { get; set; }
}

Which yields the following:

{
"values":[{"1":"one"},{"2":"two"},{"3":"three"},{"4":"four"}]
}
Sign up to request clarification or add additional context in comments.

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.