Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,
I have a JSON file, trying to deserialize it using JSON.Net.
I noticed when trying to import certain part is working very well, but other is not, only showing when debugging number of rows(when converting to DataTable) but the table itself is empty.
Here is the code to Deserialize:
C#
string url = "http://abc.ca/compat.json?vehicleid=741&modelid=123&makeid=8";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = WebRequestMethods.Http.Get;
            httpWebRequest.Accept = "application/json; charset=utf-8";
            string JSONfile;
            var response = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var sr = new StreamReader(response.GetResponseStream()))
            { JSONfile = sr.ReadToEnd(); }

            var infoTB = JsonConvert.DeserializeAnonymousType(JSONfile, new { product = default(DataTable) }).product;

Here is how Product in JSON file looks like:
JavaScript
"product": [{
		"productid": 411,
		"partnumber": "P-ONE",
		"price": ""
		-------
	}, {
		"productid": 160,
		"partnumber": "P-Two",
		"price": "99.99"
		-------
	}],

When trying to apply the same on another part of the file it gives me empty datatable
Here's how it looks like:
JavaScript
"recommend": [true, false],
	"notes": [],
	"notesV2": [false, false],
	"benefits": [
		[{
			"featureid": 271,
			"title_en": "en title",
			"title_fr": "fr title",
			"name_en": "name en",
			"name_fr": "name fr",
			"url_en": "url1",
			"url_fr": "url2"
		}, {
			"featureid": 251,
			"title_en": "en title",
			"title_fr": "fr title",
			"name_en": "name en",
			"name_fr": "name fr",
			"url_en": "url1",
			"url_fr": "url2"
		}],
		[{
			"featureid": 171,
			"title_en": "en title",
			"title_fr": "fr title",
			"name_en": "name en",
			"name_fr": "name fr",
			"url_en": "url1",
			"url_fr": "url2"
		}, {
			"featureid": 271,
			"title_en": "en title",
			"title_fr": "fr title",
			"name_en": "name en",
			"name_fr": "name fr",
			"url_en": "url1",
			"url_fr": "url2"
		}]
	],

I'm trying to read "benefits" but it counts the rows without data.
Please advise why it creates empty datatable in the second one

Thanks,
Samira
Posted
Comments
Sergey Alexandrovich Kryukov 4-Dec-15 16:14pm    
So, what you may possibly want? It looks like you start processing files with totally different schema. No wonder old code doesn't work. Perhaps you have to do the same work you've done with the old schema. Hope there is a way to tall one from another. If not you are in big trouble.
—SA
Samira Radwan 4-Dec-15 21:44pm    
both schema in my question are in the same file!
Sergey Alexandrovich Kryukov 5-Dec-15 1:44am    
Well, perhaps you did not show it. Well, good. But if you did not have old and new, your old code would work. So, you still have old and new schema, even if your new schema, let's assume that, includes a new one. You have to update your software.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900