1

I'm using HttpWebRequest method to "GET" data from a specific url that the returned data supposed to be in json format. My code are something like

  WebRequest request = WebRequest.Create("https://xxx.xxxxxxxx.com/xxxxxxx");
    request.Method = "GET";
    request.ContentType = "application/json";
    var response = (HttpWebResponse)request.GetResponse();
    using (var streamReader = new StreamReader(response.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();

    }

The responseText value as I observed are

 [
 {
 "webinarKey":5303085652037254656,
 "subject":"Test+Webinar+One",
 "description":"Test+Webinar+One+Description",
 "organizerKey":73563532324,
 "times":[{"startTime":"2011-04-26T17:00:00Z","endTime":"2011-04-26T18:00:00Z"}]
 },
 {
 "webinarKey":9068582024170238208,
 "name":"Test+Webinar+Two",
 "description":"Test Webinar Two Description",
 "organizerKey":73563532324,
 "times":[{"startTime":"2011-04-26T17:00:00Z","endTime":"2011-04-26T18:00:00Z"}]
 }
 ]

As you can see it is in json format, but I don't know how to set it as a json object, so I can get it's field value as something like

string webinarKey=responseText[0].webinarKey;

Am I right?

Steven Zack
  • 4,984
  • 19
  • 57
  • 88

5 Answers5

0

You have to parse the response (that is a textstring with JSON) with a JSON parser/deserializer. For example: Json.net

RvdK
  • 19,580
  • 4
  • 64
  • 107
0

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

WCF has a DataContractJSONDeserializer.

You would need to define your types as .net objects that had properties that "look" like the json data coming back. I don't get the impression that you are actually using WCF in your application but you may still be able to use the DataContractJSONDeserializer anyway. You just need to instruct it the type you want it to deserialze as and the type needs to be marked with a DataContract attribute.

Heres plenty more info

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

Michael Christensen
  • 1,768
  • 1
  • 13
  • 11
0

You can almost always get away with using the JavaScriptSerializer class. There will be numerous variations on this, and I can see suggestions in other answers already, though this might well suffice. Namely, you'll want to look into the Deserialize<T> method, with a signature of:

public T Deserialize<T>(
  string input
)

One advantage, if this suits, is that it is a readily available class in System.Web.Extension and removes the requirement for 'third party components'.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

One possibility for this is to use a JObject instance. YOu can pass it a string and then extract the values easily:

JObject jobj = JObject.Parse(resultString);
someValue = jobj[0]["webinarKey"];
Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
0
JavaScriptSerializer ser = new JavaScriptSerializer();
MyClass package = null;   
package = ser.Deserialize<MyClass>(item);

Where item is your response text, and MyClass is the .net class you are returning. then you can access the properties of your object.

FiveTools
  • 5,970
  • 15
  • 62
  • 84