Problem trying to deserialize a json with a model - c#
I am trying to deserialize a json (using Newtonsoft.Json), i created the classes, but in json code there is a variable that i don't know if it is corrected or i don't know how to deserialize.
I am getting a null exception on line : MessageBox.Show(root.matriculations._14000.name)
private void btnImportaDados_Click(object sender, EventArgs e)
{
string getDataUrl = "https://xxx.xxx.com/api/matriculations/get.json?entity_code=14000";
try
{
using (var webClient = new System.Net.WebClient())
{
var json = webClient.DownloadString(getDataUrl);
var root = JsonConvert.DeserializeObject<Rootobject>(json);
MessageBox.Show(root.matriculations._14000.name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public class Rootobject
{
public Matriculations matriculations { get; set; }
}
public class Matriculations
{
public _14000 _14000 { get; set; }
}
public class _14000
{
public string entity_code { get; set; }
public string name { get; set; }
public string street { get; set; }
public string local { get; set; }
public string postal_code { get; set; }
public string sub_postal_code { get; set; }
public string phone1 { get; set; }
public object phone2 { get; set; }
public string email1 { get; set; }
public object email2 { get; set; }
public string entity_code_2 { get; set; }
public string courseaction_ref { get; set; }
public string courseaction_id { get; set; }
public string course_code { get; set; }
public string course { get; set; }
public Billingorderplan[] billingorderplans { get; set; }
}
public class Billingorderplan
{
public string id { get; set; }
public string paid { get; set; }
public string date { get; set; }
public object obs { get; set; }
public object payment_doc_num { get; set; }
public string value { get; set; }
public string description { get; set; }
public string entity_code { get; set; }
public object paid_date { get; set; }
}
The result(json data) from the url is:
{"matriculations":{"14000":{"entity_code":"14000","name":"Fabio Danilson Sivone Antonio","street":"Rua Dr. Pereira Jardim, Bl. 3 - 25 - 4 B","local":"Luanda","postal_code":"2685","sub_postal_code":"093","phone1":"923810539","phone2":null,"email1":"fabiodanilson1#hotmail.com","email2":null,"entity_code_2":"9957","courseaction_ref":"EPCE_01","courseaction_id":"1828","course_code":"EPCE","course":"Especializa\u00e7\u00e3o Avan\u00e7ada em Investiga\u00e7\u00e3o de Crime Econ\u00f3mico [E-learning]","billingorderplans":[{"id":"298","paid":"0","date":"2020-05-06","obs":null,"payment_doc_num":null,"value":"0.00","description":"Inscri\u00e7\u00e3o","entity_code":"14000","paid_date":null},{"id":"299","paid":"0","date":"2019-11-21","obs":null,"payment_doc_num":null,"value":"0.00","description":"1\u00aa presta\u00e7\u00e3o","entity_code":"14000","paid_date":null},{"id":"300","paid":"0","date":"2019-12-08","obs":null,"payment_doc_num":null,"value":"0.00","description":"2\u00aa presta\u00e7\u00e3o","entity_code":"14000","paid_date":null},{"id":"301","paid":"0","date":"2020-01-08","obs":null,"payment_doc_num":null,"value":"0.00","description":"3\u00aa presta\u00e7\u00e3o","entity_code":"14000","paid_date":null},{"id":"302","paid":"0","date":"2020-02-08","obs":null,"payment_doc_num":null,"value":"0.00","description":"4\u00aa presta\u00e7\u00e3o","entity_code":"14000","paid_date":null},{"id":"303","paid":"0","date":"2020-03-08","obs":null,"payment_doc_num":null,"value":"0.00","description":"5\u00aa presta\u00e7\u00e3o","entity_code":"14000","paid_date":null},{"id":"304","paid":"0","date":"2020-04-08","obs":null,"payment_doc_num":null,"value":"0.00","description":"6\u00aa presta\u00e7\u00e3o","entity_code":"14000","paid_date":null},{"id":"305","paid":"0","date":"2020-05-08","obs":null,"payment_doc_num":null,"value":"0.00","description":"7\u00aa presta\u00e7\u00e3o","entity_code":"14000","paid_date":null},{"id":"306","paid":"0","date":"2020-06-08","obs":null,"payment_doc_num":null,"value":"0.00","description":"8\u00aa presta\u00e7\u00e3o","entity_code":"14000","paid_date":null},{"id":"16595","paid":"0","date":"2020-08-27","obs":null,"payment_doc_num":null,"value":"20.00","description":"EExt - Atividade","entity_code":"14000","paid_date":null},{"id":"16596","paid":"0","date":"2020-08-27","obs":null,"payment_doc_num":null,"value":"45.00","description":"EExt - Teste","entity_code":"14000","paid_date":null},{"id":"16597","paid":"0","date":"2020-08-27","obs":null,"payment_doc_num":null,"value":"20.00","description":"EExt - Atividade","entity_code":"14000","paid_date":null},{"id":"16598","paid":"0","date":"2020-08-27","obs":null,"payment_doc_num":null,"value":"45.00","description":"EExt - Teste","entity_code":"14000","paid_date":null},{"id":"16599","paid":"0","date":"2020-08-27","obs":null,"payment_doc_num":null,"value":"20.00","description":"EExt - Atividade","entity_code":"14000","paid_date":null},{"id":"16601","paid":"0","date":"2020-08-27","obs":null,"payment_doc_num":null,"value":"45.00","description":"EExt - Teste","entity_code":"14000","paid_date":null},{"id":"16600","paid":"0","date":"2020-08-27","obs":null,"payment_doc_num":null,"value":"20.00","description":"EExt - Atividade","entity_code":"14000","paid_date":null}]}}}
But that "14000" is a varible value depending from the parameter passed.
How can i do it?
Thank You.
Since 14000 cannot be a variable name you will have to resort to some manual intervention to capture that dynamic value. The cleanest way is to remove the Matriculations class and replace it with Dictionary<int, _14000> (or Dictionary<string, _14000>).
public class Rootobject
{
public Dictionary<int, _14000> matriculations { get; set; }
}
This will ensure that the key is captured correctly even if it is a number. You can read the object from the dictionary's values as such: root.matriculations.Values.First().name. (Remember to import System.Linq for using .First().)
For clarity you can rename _14000 to something more descriptive.
You do not have _14000 in returned JSON but 14000 under matriculations.
Related
Json Deserialization | Cannot Deserialize Current Json
I have below Json - {"property_id":"53863730","name":"Hayat Elhamra","address":{"line_1":"Jeddah","city":"Jeddah","state_province_name":"Jeddah","postal_code":"23212","country_code":"SA","obfuscation_required":false,"localized":{"links":{"ar-SA":{"method":"GET","href":"https://api.ean.com/2.4/properties/content?language=ar-SA&property_id=53863730&include=address"}}}},"location":{"coordinates":{"latitude":21.520902,"longitude":39.158265}},"phone":"20-01145772035","category":{"id":"16","name":"Apartment"},"rank":99999999,"business_model":{"expedia_collect":true,"property_collect":true},"dates":{"added":"2020-06-10T23:03:21.345Z","updated":"2020-06-10T23:03:23.701Z"},"chain":{"id":"0","name":"Independent"},"brand":{"id":"0","name":"Independent"}} {"property_id":"53183065","name":"Carefully Furnished Bungalow With 2 Bathrooms, 7km From Pula","address":{"line_1":"1 x M 90,3","line_2":"PRIVATE_VACATION_HOME 3","city":"Fazana","state_province_name":"Istria (county)","postal_code":"52212","country_code":"HR","obfuscation_required":true,"localized":{"links":{"hr-HR":{"method":"GET","href":"https://api.ean.com/2.4/properties/content?language=hr-HR&property_id=53183065&include=address"}}}},"ratings":{"property":{"rating":"3.0","type":"Star"}},"location":{"coordinates":{"latitude":44.93,"longitude":13.8}},"phone":"410442743080","category":{"id":"17","name":"Private vacation home"},"rank":99999999,"business_model":{"expedia_collect":true,"property_collect":false},"dates":{"added":"2020-05-13T21:06:42.861Z","updated":"2020-05-18T21:57:39.242Z"},"statistics":{"1073743378":{"id":"1073743378","name":"Number of bedrooms - 2","value":"2"},"1073743380":{"id":"1073743380","name":"Max occupancy - 4","value":"4"},"1073743379":{"id":"1073743379","name":"Number of bathrooms - 2","value":"2"}},"chain":{"id":"7278","name":"Belvilla"},"brand":{"id":"7353","name":"Belvilla"}} {"property_id":"53182898","name":"Snug Cottage in Pašman With Roofed Terrace","address":{"line_1":"Pasman","city":"Pasman","state_province_name":"Zadar","postal_code":"23260","country_code":"HR","obfuscation_required":true,"localized":{"links":{"hr-HR":{"method":"GET","href":"https://api.ean.com/2.4/properties/content?language=hr-HR&property_id=53182898&include=address"}}}},"ratings":{"property":{"rating":"1.0","type":"Star"}},"location":{"coordinates":{"latitude":43.891571,"longitude":15.423619}},"phone":"410442743080","category":{"id":"11","name":"Cottage"},"rank":99999999,"business_model":{"expedia_collect":true,"property_collect":false},"dates":{"added":"2020-05-13T21:13:49.155Z","updated":"2020-05-27T21:02:31.808Z"},"statistics":{"1073743378":{"id":"1073743378","name":"Number of bedrooms - 2","value":"2"},"1073743380":{"id":"1073743380","name":"Max occupancy - 5","value":"5"},"1073743379":{"id":"1073743379","name":"Number of bathrooms - 1","value":"1"}},"chain":{"id":"7278","name":"Belvilla"},"brand":{"id":"7353","name":"Belvilla"}} For this I have created below class structure - public class Property { public string property_id { get; set; } public string name { get; set; } public Address address { get; set; } public Location location { get; set; } public string phone { get; set; } public Category category { get; set; } public int rank { get; set; } public Business_Model business_model { get; set; } public Dates dates { get; set; } public Chain chain { get; set; } public Brand brand { get; set; } } public class Address { public string line_1 { get; set; } public string city { get; set; } public string state_province_name { get; set; } public string postal_code { get; set; } public string country_code { get; set; } public bool obfuscation_required { get; set; } public Localized localized { get; set; } } public class Localized { public Links links { get; set; } } public class Links { public ArSA arSA { get; set; } } public class ArSA { public string method { get; set; } public string href { get; set; } } public class Location { public Coordinates coordinates { get; set; } } public class Coordinates { public float latitude { get; set; } public float longitude { get; set; } } public class Category { public string id { get; set; } public string name { get; set; } } public class Business_Model { public bool expedia_collect { get; set; } public bool property_collect { get; set; } } public class Dates { public DateTime added { get; set; } public DateTime updated { get; set; } } public class Chain { public string id { get; set; } public string name { get; set; } } public class Brand { public string id { get; set; } public string name { get; set; } } I have below code where I am getting error - using (StreamReader streamReader = new StreamReader("d://propertycontent.expediacollect.en-US.json")) { using (var json = new JsonTextReader(streamReader)) { JsonSerializer serializer = new JsonSerializer(); var properties= (List<Property>)serializer.Deserialize(json, typeof(List<Property>)); } } Error - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Property]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'property_id', line 1, position 15.'
It is not deserializing because it is not valid json. To make it valid, and to make it a List<Property> add [ to the beginning of the json and ] to the end of the json. Just enclose the json in [ ... ] to make it valid and it will deserialize assuming the rest of it is valid and is not missing any commas or brackets.
try this, you will have to install NewtonsoftJson. It was tested using Visual Studio and Postman and works properly. var jsonOrig= ...your json var json = JsonConvert.SerializeObject(jsonOrig); var jsonObj = JsonConvert.DeserializeObject<DataRoot>(json); classes public class DataRoot { public string property_id { get; set; } public string name { get; set; } public Address address { get; set; } public Location location { get; set; } public string phone { get; set; } public Category category { get; set; } public int rank { get; set; } public BusinessModel business_model { get; set; } public Dates dates { get; set; } public Chain chain { get; set; } public Brand brand { get; set; } } public class ArSA { public string method { get; set; } public string href { get; set; } } public class Links { [JsonProperty("ar-SA")] public ArSA ArSA { get; set; } } public class Localized { public Links links { get; set; } } public class Address { public string line_1 { get; set; } public string city { get; set; } public string state_province_name { get; set; } public string postal_code { get; set; } public string country_code { get; set; } public bool obfuscation_required { get; set; } public Localized localized { get; set; } } public class Coordinates { public double latitude { get; set; } public double longitude { get; set; } } public class Location { public Coordinates coordinates { get; set; } } public class Category { public string id { get; set; } public string name { get; set; } } public class BusinessModel { public bool expedia_collect { get; set; } public bool property_collect { get; set; } } public class Dates { public DateTime added { get; set; } public DateTime updated { get; set; } } public class Chain { public string id { get; set; } public string name { get; set; } } public class Brand { public string id { get; set; } public string name { get; set; } } `````
C# MVC can't Deserialize a tuple
I have a Json model that looks like this private class SearchMetadataJson { public string entertain { get; set; } public string master { get; set; } public string memail { get; set; } public string key { get; set; } public (int, string)[] mood { get; set; } public int? soundnumber { get; set; } public int? ftv { get; set; } public int? com { get; set; } public (int, string)[] sims { get; set; } public (int, string)[] keysecond { get; set; } public string popt { get; set; } public (string, string) syncs { get; set; } } And I try to de-serialize the object like this var CommentObj = JsonSerializer.Deserialize<SearchMetadataJson>(CommentAsString); The data that I'm trying to de-serialize (aka "CommentAsString") looks like this "{\"entertain\":\"PEG\",\"master\":\"Phos Ent Group\",\"memail\":\"example#example.com\",\"key\":\"Db\",\"mood\":{\"1\":\"TypeA\",\"4\":\"TypeB\",\"5\":\"TypeC\"},\"soundnumber\":\"5\",\"ftv\":\"4\",\"com\":\"3\",\"sims\":{\"1\":\"Band1\",\"2\":\"Band2\"},\"keysecond\":{\"1\":\"KeyWord1\",\"2\":\"KeyWord2\",\"3\":\"KeyWord3\"},\"syncs\":{\"Other pubber\":\"example2#example.com\"}}" But I keep getting this error Does anyone see what the problem is? Update The integers in CommentAsString are variables and will be different every time the function is called so I can't make a Json Object that has a key value of a particular integer.
Let's look at the actual formatted data structure { "entertain":"PEG", "master":"Phos Ent Group", "memail":"example#example.com", "key":"Db", "mood":{ "1":"TypeA", "4":"TypeB", "5":"TypeC" }, "soundnumber":"5", "ftv":"4", "com":"3", "sims":{ "1":"Band1", "2":"Band2" }, "keysecond":{ "1":"KeyWord1", "2":"KeyWord2", "3":"KeyWord3" }, "syncs":{ "Other pubber":"example2#example.com" } } Converting these to an array of tuple would be unusual. What you seemingly have are dictionary's Example private class SearchMetadataJson { public string entertain { get; set; } public string master { get; set; } public string memail { get; set; } public string key { get; set; } public Dictionary<int,string> mood { get; set; } public int? soundnumber { get; set; } public int? ftv { get; set; } public int? com { get; set; } public Dictionary<int,string> sims { get; set; } public Dictionary<int,string> keysecond { get; set; } public string popt { get; set; } // public (string, string) syncs { get; set; } } It's debatable whether the last property is an object or another dictionary as well. "syncs":{ "Other pubber":"example2#example.com" } However, I'll leave that up to you.
your have error in the model, use this site for convert your json in c# https://json2csharp.com/ // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); public class Mood { public string _1 { get; set; } public string _4 { get; set; } public string _5 { get; set; } } public class Sims { public string _1 { get; set; } public string _2 { get; set; } } public class Keysecond { public string _1 { get; set; } public string _2 { get; set; } public string _3 { get; set; } } public class Syncs { public string Otherpubber { get; set; } } public class Root { public string entertain { get; set; } public string master { get; set; } public string memail { get; set; } public string key { get; set; } public Mood mood { get; set; } public string soundnumber { get; set; } public string ftv { get; set; } public string com { get; set; } public Sims sims { get; set; } public Keysecond keysecond { get; set; } public Syncs syncs { get; set; } } try again with using this first read your string this is the response using post, get or delete var response = await client.PostAsync("your-url", datasBody); var contentData = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }; var CommentObj = JsonSerializer.Deserialize<Root>(contentData, options); if your model is bad or not match using [JsonProperty("entertain")] public string entertain { get; set; }
You will either need to use a custom converter or convert your tuples into separate classes with fields to explain what each field is used for.
Deserializing a nested JSON string, cannot access properties
I am having issues deserializing a nested JSON array from the Genius lyric website API. I formulated the object using http://json2csharp.com. When I deserialize the object, I am unable to access the properties inside of the class, which wasn't entirely unexpected, I am just not sure how to properly design an actual solution to the problem. The JSON object conversions work fine when they are not nested. What would be the best way to go about handling this? Here is the conversion code: string test = await G.SearchGeniusASync(textBox1.Text); var data = JsonConvert.DeserializeObject<GeniusApiObject>(test); Here is my class: class GeniusApiObject { public class Meta { public int status { get; set; } } public class Stats { public bool hot { get; set; } public int unreviewed_annotations { get; set; } public int concurrents { get; set; } public int pageviews { get; set; } } public class PrimaryArtist { public string api_path { get; set; } public string header_image_url { get; set; } public int id { get; set; } public string image_url { get; set; } public bool is_meme_verified { get; set; } public bool is_verified { get; set; } public string name { get; set; } public string url { get; set; } public int iq { get; set; } } public class Result { public int annotation_count { get; set; } public string api_path { get; set; } public string full_title { get; set; } public string header_image_thumbnail_url { get; set; } public string header_image_url { get; set; } public int id { get; set; } public int lyrics_owner_id { get; set; } public string lyrics_state { get; set; } public string path { get; set; } public int? pyongs_count { get; set; } public string song_art_image_thumbnail_url { get; set; } public Stats stats { get; set; } public string title { get; set; } public string title_with_featured { get; set; } public string url { get; set; } public PrimaryArtist primary_artist { get; set; } } public class Hit { public List<object> highlights { get; set; } public string index { get; set; } public string type { get; set; } public Result result { get; set; } } public class Response { public List<Hit> hits { get; set; } } public class RootObject { public Meta meta { get; set; } public Response response { get; set; } } } This is the source for the SearchGeniusASync method in case it is helpful: public async Task<string>SearchGeniusASync(string searchParameter) { httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", clientAccessToken); var result = await httpClient.GetAsync(new Uri("https://api.genius.com/search?q=" + searchParameter), HttpCompletionOption.ResponseContentRead); var data = await result.Content.ReadAsStringAsync(); return data; } This is the scope I am given access to: https://i.imgur.com/9mZMvfp.png Here's a sample JSON request in plaintext: https://pastebin.com/iA8dQafW
GeniusApiObject is not needed in the code, but I'll leave it in just because it helps organize things (may be that something else also has a RootObject from the auto-generator). The problem is that you are trying to deserialize to what is just an empty class, the class itself has no properties, so you can't deserialize to it. You need to deserialize to the GeniusApiObject.RootObject. var data = JsonConvert.DeserializeObject<GeniusApiObject.RootObject>(test); Will deserialize to the .RootObject subclass. This is verified working: Where I'm using File.ReadAllText("test.json") to load the example API data provided. Here is a .NET Fiddle showing it working (without the root object and only one song in the response). Thanks to #maccttura.
Why my deserialization is giving me an error Newtonsoft.Json.JsonConvert?
public List<CoinMarket> GetCoinMarket() { List<CoinMarket> coinMarket = new List<CoinMarket>(); var URLWebAPI = "http://190.202.54.19/wsZeus/api/Account/Markets/Get"; try { using (var Client = new System.Net.Http.HttpClient()) { var JSON = Client.GetStringAsync(URLWebAPI); coinMarket = (List<CoinMarket>)Newtonsoft.Json.JsonConvert.DeserializeObject(JSON.Result); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(#" ERROR {0}", ex.Message); } return coinMarket; } It is throwing and i do not know why. It looks like there is something wrong with the serialization part. But i verified it.
You are using json deserializer incorrectly. DeserializeObject returns custom object which is not castable to your List<T>. This output: Newtonsoft.Json.Linq.JArray SO20171129.CoinData[] System.Collections.Generic.List`1[SO20171129.CoinData] is the result of this code. class Program { static void Main(string[] args) { // returns Newtonsoft.Json.Linq.JArray var coinMarket = Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText("get.json")); Console.WriteLine(coinMarket.GetType()); // returns array of CoinData var coinMarketTyped = Newtonsoft.Json.JsonConvert.DeserializeObject<CoinData[]>(File.ReadAllText("get.json")); Console.WriteLine(coinMarketTyped.GetType()); // returns List of CoinData var coinMarketTyped2 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CoinData>>(File.ReadAllText("get.json")); Console.WriteLine(coinMarketTyped2.GetType()); } } public class CoinData { public string id { get; set; } public string name { get; set; } public string symbol { get; set; } public string rank { get; set; } public string price_usd { get; set; } public string price_btc { get; set; } public string __invalid_name__24h_volume_usd { get; set; } public string market_cap_usd { get; set; } public string available_supply { get; set; } public string total_supply { get; set; } public string percent_change_1h { get; set; } public string percent_change_24h { get; set; } public string percent_change_7d { get; set; } public string last_updated { get; set; } }
My guess is that your json structure members do not match the CoinMarket class members. What I would do is to copy the json result and generate the corresponding CoinMarket class on the following website: http://json2csharp.com/ It will output the right CoinMarket class for you.
public class CoinMarket { public string id { get; set; } public string name { get; set; } public string symbol { get; set; } public string rank { get; set; } public string price_usd { get; set; } public string price_btc { get; set; } [JsonProperty("24h_volume_usd")] public string h_volume_usd { get; set; } public string market_cap_usd { get; set; } public string available_supply { get; set; } public string total_supply { get; set; } public string percent_change_1h { get; set; } public string percent_change_24h { get; set; } public string percent_change_7d { get; set; } public string last_updated { get; set; } } } This is my CoinMarket Class
Windows Phone Parse json data? from Url with user input?
I Followed this to Create Restful Web Services which display JSON as OutPut from the MySQL Data base. I Successfully Done it, But Here I Have nearly 100 Tables in Database with different name And I am Getting This kind of Json Data {"WindowsResult":[{"ID":9,"TYPE":"WindowsPhone","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post1.jpg"}],"success":3} this another result {"SonyResult":[{"ID":3,"TYPE":"SonyXperia","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post2.jpg"}],"success":3} this another one {"SamsungResult":[{"ID":1,"TYPE":"Samsung","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post3.jpg"}],"success":3} For this I am parsing a JSON data form a url like localhost/Service1.svc/windows or localhost/Service1.svc/sony like that Here it should Take with a user input.. private void btnAdd_Click(object sender, RoutedEventArgs e) { string Data = txtData.Text; string ServiceUri = "http://lhost:30576/Service1.svc/" + "Data" + "/"; WebClient proxy = new WebClient(); proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted); proxy.DownloadStringAsync(new Uri(ServiceUri)); } But I am Confused at Here void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result); foreach (var TYPE in rootObject.SonyResult) { Console.WriteLine(type.TITLE); } } what should be at Root object for some Results I have SamsungResult as Root Object like that. Please suggest Me Regarding this.. Update when I enter the a url like http://www.google.com/service1.scv/sony I am getting {"SonyResult":[{"ID":3,"TYPE":"SonyXperia","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post2.jpg"}],"success":3} like that every brand I have one result Not All in one string..
K bro the problem is in your json data {"Mobiles":[ {"SonyResult":[{"ID":3,"TYPE":"SonyXperia","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post2.jpg"}],"success":3}, {"WindowsResult":[{"ID":9,"TYPE":"WindowsPhone","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post1.jpg"}],"success":3} ]} Since you hadn't specified a Root object name for our tables so json was using your table names as root objects. Its C# class will be public class SonyResult { public int ID { get; set; } public string TYPE { get; set; } public string TITLE { get; set; } public string PRICE { get; set; } public string IMAGE { get; set; } } public class WindowsResult { public int ID { get; set; } public string TYPE { get; set; } public string TITLE { get; set; } public string PRICE { get; set; } public string IMAGE { get; set; } } public class Mobile { public List<SonyResult> SonyResult { get; set; } public int success { get; set; } public List<WindowsResult> WindowsResult { get; set; } } public class RootObject { public List<Mobile> Mobiles { get; set; } } Hope that solved your problem. Update For ur received query the C# class is going to be public class SonyResult {public int ID { get; set; } public string TYPE { get; set; } public string TITLE { get; set; } public string PRICE { get; set; } public string IMAGE { get; set; } } public class RootObject { public List<SonyResult> SonyResult { get; set; } public int success { get; set; } } Now since for each different models you are getting different queries where each are an array of its own with no common object name. So for each different model the root object class will differ. I havent tried it as I dont have access to your code but try if this format works(Since I havent tried this so I have my doubts if this will work or not). public class SonyResult { public int ID { get; set; } public string TYPE { get; set; } public string TITLE { get; set; } public string PRICE { get; set; } public string IMAGE { get; set; } } public class WindowsResult { public int ID { get; set; } public string TYPE { get; set; } public string TITLE { get; set; } public string PRICE { get; set; } public string IMAGE { get; set; } } public class RootObject { public List<SonyResult> SonyResult { get; set; } public int success { get; set; } public List<WindowsResult> WindowsResult { get; set; } }