1

The serialization of the array returns the following JSON:

[{"Code":"AAAA","Description":"Description of AAAA"},{"Code":"BBBB","Description":"Description of BBBB"}]

My goal is to return the following JSON:

{"AAAA":{"Description":"Description of AAAA"},"BBBB":{"Description":"Description of BBBB"}}
0

3 Answers 3

3

You can achieve something simliar (not exactly the same you are expecting) if instead of serializing an array, build a temporary Dictionary and serialize it.

var dict = new Dictionary<String, YourClass>();
foreach (YourClass yourObj in listOfObjs)
{
    dict[yourObj.Code] = yourObj;
}
// then serialize "dict"

You could add a rule to your JSON serializer to make it avoid serializing "code" property in YourClass, and you will end up with a JSON object exactly as you show in your example.

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

1 Comment

Or just make an object representing the serialized target (leaving the original app object alone).
1

You'll need to either use a class that has the "AAAA" and "BBBB" properties, or you'll need to serialize a dictionary instead. As it is, you're serializing an array and getting an array.

Comments

0

The table on this blog post shows several search starting points.

.Net has built-in System.Web.Script.Serialization.JavaScriptSerializer, here, with examples

The .Net one doesn't specially serialize Dictionary the way you want, but some of the others do, I believe. However, I think there are reasons NOT to want a general serialization routine the way you've requested - though I can't list them certainly.

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.