Answered by:
Loop JSON object

Question
-
User-2021793694 posted
Hi,
I have below JSON data (it consists of thousand of record)
{ "items":[ { "20057":{ "name":"Name AA", "item_Desc":"AA Desc" } }, { "20060":{ "name":"Name BB", "item_Desc":"BB Desc" } } ] }
And i need to insert it into a database with below formatItemID | Name | Item_Desc ---------------------------------- 20057 | Name AA | AA Desc 20060 | Name BB | BB Desc
This is what i have right now.
StreamReader streamReader = new StreamReader(@"~\data\test-data.json"); string text = streamReader.ReadToEnd(); streamReader.Close(); JavaScriptSerializer ser = new JavaScriptSerializer(); var jObj = (JObject)JsonConvert.DeserializeObject(text); var result = jObj["items"].Select(item => new { name = item["20057"]["name"], //test with hardcoded item id //TO DO }).ToList(); foreach (var item in result) { //insert into database }
I am having problem in getting the Item ID as it is a "key" not a "value".I need your help to improve on the "TO DO" part so that I can add all the item details into a list.
Sunday, October 13, 2013 5:17 PM
Answers
-
User-488622176 posted
Use the NewtonSoft JSON parser freely available on CodePlex(http://json.codeplex.com/).
See this link for iterating : http://james.newtonking.com/json/help/index.html
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 14, 2013 5:11 AM
All replies
-
User220959680 posted
Generate strong Type using csharptojson.com, follow my article at
Sunday, October 13, 2013 5:30 PM -
User-2021793694 posted
Yes i am following your method but the problem is each of the item consists of a unique key(20057,20060...). In my example I hardcoded the item id in order to get the name(item["20057"]["name"]). But this only work for a single record. So, how can i get each of them dynamically?
Sunday, October 13, 2013 7:36 PM -
User-488622176 posted
Use the NewtonSoft JSON parser freely available on CodePlex(http://json.codeplex.com/).
See this link for iterating : http://james.newtonking.com/json/help/index.html
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 14, 2013 5:11 AM