locked
Loop JSON object RRS feed

  • 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 format

    ItemID | 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

All replies