tags:

views:

127

answers:

2

I'm trying to generate JSON using C# and DataContractJsonSerializer in .Net 3.5. The problem is that I can't figure out how to build the structure correct for the result I need.

I've tried to reproduce PHP's associative arrays using both hashtables, list objects and arraylists but can't figure out how to generate my result in the best way using DataContractJsonSerializer without having to create my own recursive loop for building JSON.

The closest approach is using the a Dictionary < string, Dictionary < string, string>> aproach but the result is too big as I can't "name" the keys.

This is what I get:

[
   {
      "Key":"status",
      "Value":[
         {
            "Key":"value",
            "Value":"ok"
         }
      ]
   },
   {
      "Key":"1",
      "Value":[
         {
            "Key":"name",
            "Value":"Result1"
         },
         {
            "Key":"details",
            "Value":"Result 1 details"
         }
      ]
   },
   {
      "Key":"2",
      "Value":[
         {
            "Key":"name",
            "Value":"Result2"
         },
         {
            "Key":"details",
            "Value":"Result 2 details"
         }
      ]
   },
   {
      "Key":"caller",
      "Value":[
         {
            "Key":"value",
            "Value":"1135345"
         }
      ]
   }
]

This is what I want:

{
   "status":"ok",
   "response":[
      {
         "id":1,
         "name":"Result1"
         "details":"Result 1 details"
      },
      {
         "id":2,
         "name":"Result2"
         "details":"Result 2 details"
      },
      {
         "id":3,
         "name":"Result3"
         "details":"Result 3 details"
    ],
   "caller":"1135345"
}

Does anybody have a clue how I can generate this piece of JSON using C# without having to load the entire Json.NET framework? I need this to be generated as fast as possible as this project aims to become an site search engine.

+1  A: 

Using the JavaScriptSerializer rather than DataContractJsonSerializer on a Dictionary will remove the Key/Value json attributes and make them into a "keyed" array.

http://msdn.microsoft.com/en-us/library/bb412168.aspx

Have you attributed your classes with the [DataContract] and [DataMember] decorators?

http://msdn.microsoft.com/en-us/library/bb412179.aspx

BozoJoe
+1  A: 

You should look at using the JavaScriptSerializer. It's part of the .NET framework (distributed in System.Web.Extensions). To get the result you want you can do this:

var results = new[]
{
    new{id=1,name="Result 1"},
    new{id=2,name="Result 2"},
    new{id=3,name="Result 3"}
};

var js = new JavaScriptSerializer();
var result = js.Serialize(new
{
    status = "ok",
    response = results,
    caller = 1135345
});

You can either use anonymous classes, or any existing ones. Works perfectly fine :) The return value of this call would be:

{"status":"ok","response":[{"id":1,"name":"Result 1"},{"id":2,"name":"Result 2"},{"id":3,"name":"Result 3"}],"caller":1135345}
Artiom Chilaru