2
{"status":1,"msg":"Success","details":{"123456789":{"id":"4029292","name":"ram"}}}

This is my string (I think json array not sure). I got this as response from webservice.

How do I get elements from this?

ronalchn
  • 12,225
  • 10
  • 51
  • 61
balaji
  • 1,236
  • 2
  • 18
  • 28
  • I think you need to add more details. Are you using jquery? Are you using ajax? – Nick Sep 05 '12 at 06:57
  • How do you plan to consume this web service? c#, javascript? – nunespascal Sep 05 '12 at 07:03
  • WebRequest request = WebRequest.Create("url"); postdata = "jxsdklgjskdgsdkgl..." bytedata = Encoding.UTF8.GetBytes(postdata); request.ContentLength = bytedata.Length; request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; Stream dataStream = request.GetRequestStream(); dataStream.Write(bytedata, 0, bytedata.Length); dataStream.Close(); WebResponse response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); String resp = reader.ReadToEnd(); // reps=response string – balaji Sep 05 '12 at 07:15
  • i need to parse the elements in codebehind – balaji Sep 05 '12 at 07:16

3 Answers3

4

If you are trying to parse it in C#, try using newtonsoft json.net for parsing json response.

It is simple and easy

Following are some references

Link1

Link2

Link3

Raghuveer
  • 2,630
  • 3
  • 29
  • 59
2

Take a look at the Json.Net library.
You can use it to deserialize json in both c# and javascript.

Message deserializedProduct = JsonConvert.DeserializeObject<Message>(json);

The Message class would have to map to response you are getting.

nunespascal
  • 17,584
  • 2
  • 43
  • 46
1

Below is my code to append a json response to the listview.It may be a good example. Also try this (Javascript):

data.msg[key] or
data.msg.123456789.id

$.each(data.response, function(key, value) {
            html += '<li><a class=contact href="#" id="' + data.response[key].id + '" ><h1>' + data.response[key].label + '</h1><p>'+ data.response[key].customerName + '</p><p>' + data.response[key].phone + ', ' + data.response[key].email + '</p></a></li>';
            $('#ul_id').append($(html));
            html='';
            console.log('conatct');
            });
Piotr Krysiak
  • 2,815
  • 3
  • 23
  • 35