-3

I deserialaize my json to a model class fine but i want to do some work on the boxes array before saving to sqlite, I'm stuck on looping through all the boxes and get the values.

{
"data": [ // single outer array
    {
        "id": 8620379, 
        "business_id": 191, 
        "business_name": "yada", 
        "boxes": [
            {
                "box_id": 485, 
                "box_name": "5/6", 
                "box_group": null
            }, 
            {
                "box_id": 483, 
                "box_name": "1/2", 
                "box_group": null
            }, 
            {
                "box_id": 484, 
                "box_name": "3/4", 
                "box_group": null
            }
        ]
    }, 
    {
        "id": 8636759, 
        "business_id": 257, 
        "business_name": "something else", 
        "boxes": [
            {
                "box_id": 1176, 
                "box_name": "FC", 
                "box_group": null
            }
        ]
    }, // and more boxes
vitr
  • 6,766
  • 8
  • 30
  • 50
user3550256
  • 181
  • 1
  • 2
  • 9
  • Take a look at this and perhaps you'll get the gist: http://stackoverflow.com/questions/6244149/parse-json-string-using-json-net – Mohsen Kamrani Sep 15 '16 at 01:37

1 Answers1

0
  1. Create a model for the JSON object. (http://json2csharp.com/)
public class Box
{
    public int box_id { get; set; }
    public string box_name { get; set; }
    public object box_group { get; set; }
}

public class Datum
{
    public int id { get; set; }
    public int business_id { get; set; }
    public string business_name { get; set; }
    public List<Box> boxes { get; set; }
}

public class RootObject
{
    public List<Datum> data { get; set; }
}
  1. Using Json.NET JSON deserializer,
RootObject obj = JsonConvert.DeserializeObject<RootObject>("your json string"); 
Adersh M
  • 596
  • 3
  • 19
  • Yes, i do that, but sqlite can't use List boxes, i need to loop through all boxes and insert datum id to the box table along with the other fields, so for each datum id select all boxes, add datum id and do a sql insert – user3550256 Sep 17 '16 at 20:26
  • Try convert the list into an array using List.ToArray() method. Then iterate the collection using foreach loop... – Adersh M Sep 18 '16 at 04:51