Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Deserialize Zomato JSON response in C#

RoyNasr:

I am using Zomato API and I am receiving the following JSON response :


{
"categories": [
{
"categories": {
"id": 1,
"name": "Delivery"
}
},
{
"categories": {
"id": 2,
"name": "Dine-out"
}
},
{
"categories": {
"id": 3,
"name": "Nightlife"
}
},
{
"categories": {
"id": 4,
"name": "Catching-up"
}
},
{
"categories": {
"id": 5,
"name": "Takeaway"
}
},
{
"categories": {
"id": 6,
"name": "Cafes"
}
},
{
"categories": {
"id": 7,
"name": "Daily Menus"
}
},
{
"categories": {
"id": 8,
"name": "Breakfast"
}
},
{
"categories": {
"id": 9,
"name": "Lunch"
}
},
{
"categories": {
"id": 10,
"name": "Dinner"
}
},
{
"categories": {
"id": 11,
"name": "Pubs & Bars"
}
},
{
"categories": {
"id": 13,
"name": "Pocket Friendly Delivery"
}
},
{
"categories": {
"id": 14,
"name": "Clubs & Lounges"
}
}
]
}

I want to Deserialize this response and I created the following classes :


public class CategoriesTop
{
public List categories { get; set; }
}
public class CategoriesBottom
{
public Categorie categorie { get; set; }
}
public class Categorie
{
public int id { get; set; }
public string name { get; set; }
}

And I am using this code to get the response :


public Form1()
{
InitializeComponent();

HttpWebRequest webRequest =
WebRequest.Create("http://ift.tt/2mIQfIW")
as HttpWebRequest;
HttpWebResponse webResponse = null;
webRequest.Headers.Add("X-Zomato-API-Key", "509ce9e4863960cdc076fb3817cc238e");
//you can get KeyValue by registering with Zomato.
webRequest.Method = "GET";
webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webResponse.StatusCode == HttpStatusCode.OK)
{
StreamReader responseReader = new
StreamReader(webResponse.GetResponseStream());
string responseData = responseReader.ReadToEnd();
//XmlDocument doc = new XmlDocument();
//doc.LoadXml(responseData);
//XmlNodeReader xmlReader = new XmlNodeReader(doc);
//DataSet ds = new DataSet();
//ds.ReadXml(xmlReader);
List l = JsonConvert.DeserializeObject>(responseData);
foreach(CategoriesBottom c in l)
{
cmbCatName.Items.Add(c.categorie.name);
}
}
}

When I run the program I receive an error on the JSON.Convert line :

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WindowsFormsApp1.CategoriesBottom]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

So I think I have a problem with the deserialize code or the classes are defined incorrectly. Any help will be appreciated.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Deserialize Zomato JSON response in C#

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×