4

I am trying to use a HTTP request to return the contents of a json string from google shopping api. What I have so far seems to be working but I would like to know how I can use the contents of the object to display the data on a page.

public string HttpGet(string url)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    try
    {
        using (Stream stream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(stream);
            return reader.ReadToEnd();
        }
     }
     finally
     {
         response.Close();
     }
 }      

 protected void submit_Click(object sender, EventArgs e)
 {
    string json = HttpGet("https://www.googleapis.com/shopping/search/v1/public/products?key={KEY}4&country=UK&q=BX80637153570K&rankBy=price:ascending&maxResults=1&alt=json");
     dynamic obj = JsonObject.Parse(json);
 }

Ok looking at the responses it looks as though I need a C# class for the data returned in json. I have created a classes using json2csharp.com. This is the data I need to return from the Json and display on the page. Maybe it will help explain my problem better.

https://www.googleapis.com/shopping/search/v1/public/products?key=AIzaSyCC0j59RBeGNtf2W2ft6avhfoTdJ1FQ2c4&country=UK&q=BX80637153570K&rankBy=price:ascending&maxResults=1&alt=json

Can anyone advise how I can use this information on my website. I am a little lost now as I'm new to all this and have tried several different methods.I don't need all of the returned data if that makes a difference? Just price and link.

user2212460
  • 89
  • 2
  • 9
  • You could use a technology like KnockoutJS to bind your JSON (as a ViewModel) to an HTML template heres a link to knockout http://knockoutjs.com/ (Hope I understood your question correctly!) – Luke Baughan Mar 28 '13 at 11:16

2 Answers2

1

Supposing dynamic obj is rightly filled you can use it's content in web page For examples, if json is:

{
  "error" : {
       "code": 400,
       "message": "Bad Request"
  } 
}

You can access object properties with code like this:

Response.Write("Error code is" + obj.error.code);

Of course Response.Write is only a sample on how you can send retrieved data to the page.

Edit 1:

It seems json converter used in question is not working or not working right. In many cases, it's overkill to create a concrete class only to parse a json, expecially since C# 4 that can use ExpandoObject

This is a sample on how you can deserialize in a dynamic object without the need to create a concrete object

    var url = "http://www.google.com/ig/calculator?hl=en&q=100USD=?EUR";
    string json = HttpGet(url);

    //this is json string:
    //{lhs: "100 U.S. dollars",rhs: "78.1799703 Euros",error: "",icc: true}

    //now convert in a dynamic object
    var jss = new DynamicJsonConverter();

    var serializer = new JavaScriptSerializer();
    serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
    dynamic obj = serializer.Deserialize(json, typeof(object));

    //now you have access to json content
    string text1 = obj.lhs;
    string text2 = obj.rhs;

DynamicJsonConverter class can be created using code you can find here:

Deserialize JSON into C# dynamic object?

Community
  • 1
  • 1
Draykos
  • 773
  • 7
  • 16
  • I tried this as suggested but it always just displays Default. So assuming your example obj.error.code is always default. – user2212460 Mar 28 '13 at 11:45
  • Then it seems you have not a working deserializer. I edited my answer, the code I added is working for me – Draykos Mar 28 '13 at 13:00
  • Thank you for your help. The code you have given me works great. I can use it to return some of the values from the json. However when I try to get the price I get an error. I have tried a few different ways such as string text1 = obj.items.price but it doesn't work. I think it is because they are in an array but I'm not sure how I can do it now – user2212460 Mar 28 '13 at 15:23
  • I don't have your json, so I'll guess. Try something like: obj.items[0].price for the first item or foreach(var item in obj.items) { var price = item.price }; if you want to iterate – Draykos Mar 28 '13 at 17:05
0

I think you need to deserialize the response

JavaScriptSerializer json_serializer = new JavaScriptSerializer();

You will need to create a class with the property resulted by the response.
Say test class

 class Test 
 {
   //define a similar property here 
   // which you suppose it will be return in the response   

 }

Then

Test routes_list = (Test)json_serializer.DeserializeObject(yourjson);

Edit 1

Or you can go through this link
How to Convert JSON object to Custom C# object?

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117