2

I am facing problem when am trying to get a JSON string like

[{Key:{key:value,key:value,key:value,key:value}, key: [{key:value,key:value,key:value,key:value}]

in C#.

my class structure is like this

        public class wideeye
    {
        public listing listing { get; set; }
        public listing_images[] listing_images { get; set; }
    }

    public class listing
    {

        public string category { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public string created_at { get; set; }
        public string current_publish_date { get; set; }
        public string details { get; set; }
        public string id { get; set; }
        public string industry { get; set; }
        public string list_exp_date { get; set; }
        public string list_price { get; set; }
        public string list_start_date { get; set; }
        public string make { get; set; }
        public string model { get; set; }
        public string open_bid { get; set; }
        public string state { get; set; }
        public string status { get; set; }
        public string title { get; set; }
        public string updated_at { get; set; }
        public string year { get; set; }
    }

    public class listing_images
    {
        public string created_at { get; set; }
        public string id { get; set; }
        public string listing_id { get; set; }
        public string listing_image_content_type { get; set; }
        public string listing_image_file_name { get; set; }
        public int listing_image_file_size { get; set; }
        public string listing_image_updated_at { get; set; }
        public string updated_at { get; set; }

    }

}

and the code is

 listing bid1 =
              new listing { category = "electronics", city = "BBSR" };

     listing_images bid2 =
             new listing_images {  created_at = "Instrumentation",  id = "10" };

     List<listing> obid1 = new List<listing>() { bid1};
     List<listing_images> obid2 = new List<listing_images>() { bid2 };

        //DataContractJsonSerializer serializer =  null;

     string sJSON = JsonConvert.SerializeObject(obid1);
     string sJSONw = JsonConvert.SerializeObject(obid2);
5
  • What are the values of sJSON and sJSONw from the code above? Commented Apr 4, 2012 at 11:50
  • sJSON and aJSONw values are coming in [{ key:value,key:value,key:value}] Commented Apr 4, 2012 at 11:53
  • can I have a string like [{"listing":{"category":"Electronics","city":"BBSR",....},"listing_images":[{"created_at":"Instrumentation","id":1,...}]} Commented Apr 4, 2012 at 11:57
  • I is hard to understand what your are trying to do since your [s and {s don't match with closing ones. Commented Apr 4, 2012 at 12:13
  • with the above code I am getting 2 json strings for sJSON am getting string as [{"category":"Electronics","city":"BBSR",....}] and for sJSONw am getting string as [{"cr‌eated_at":"Instrumentation","id":1,...}] but I need a string combining this 2 strings like [{"listing": {"category":"Electronics","city":"BBSR",....},"listing_images":[{"cr‌​eated_at":"Instrumentation","id":1,...}]}] . Hope u got this. thanx in advance Commented Apr 4, 2012 at 12:48

3 Answers 3

2

DataContractJsonSerializer class is very handy.

Add references to System.Runtime.Serialization.dll and System.Servicemodel.Web.dll to your project.

using System.Runtime.Serialization.Json;

...
...
...

    MemoryStream stream = new MemoryStream();
    DataContractJsonSerializer sr = new DataContractJsonSerializer(obj.GetType());

    sr.WriteObject(stream, obj);
    stream.Position = 0;

    StreamReader reader = new StreamReader(stream);
    string jsonResult = reader.ReadToEnd();

Of course do proper exceptions handling.

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

1 Comment

@Jrsahoo Referring to your comment in question ... why you don't then construct 3rd class which will contain both lists as members and then create object of that class and serialize it ?
1

enter image description hereI vote for using JSON.net @ http://json.codeplex.com/ Its being adopted by Microsoft and its more efficient that the DataContractJsonSerializer

example of use here : http://james.newtonking.com/projects/json/help/

Performance of Serializers

or if not use the Javascript Serializer

 protected static string JsonSerialize(Object obj)
 {
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer { MaxJsonLength = int.MaxValue };
        var json = serializer.Serialize(obj);
        return json;
  }

Comments

0

Would have left this as a comment but was rather cumbersome:

add a reference to System.Web.MVC to your project and then create the JSON like this:

var jsonOutput = Json(obid1);

This is how I generate JSON in MVC controllers for my AJAX calls. Have not tried it in a Windows mobile app though.

Just a thought.

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.