4

I need to parse some JSON data below that is nested into a datatable. the 'attributes' field for each of the 3 groups contain inner data for Title, Value, and Priority.

[{
"Title": "OVERVIEW",
"Priority": 1,
"attributes": [{
    "Title": "Type",
    "Value": "MacBook Pro",
    "Priority": 1
    },
    {
       "Title": "Operating system",
       "Value": "OS X Mountain Lion",
       "Priority": 2
    },
    {
       "Title": "Processor",
       "Value": "Intel Core i5 Processor (2.3 GHz, 3.3 GHz with TurboBoost, 6MB cache)",
       "Priority": 3
   },
   {
       "Title": "Storage",
       "Value": "500 GB HDDM 5400 rpm",
       "Priority": 4
   }]
},
{
"Title": "SPECIFICATION",
"Priority": 2,
"attributes": [{
    "Title": "RAM",
    "Value": "4 GB DDR3",
    "Priority": 1
    }]
},
{
"Title": "SCREEN",
"Priority": 3,
"attributes": [{
    "Title": "Screen size",
    "Value": "13\"",
    "Priority": 1
    }]
}]

I realise i need to deserialize the JSON data first,

List<User> UserList = JsonConvert.DeserializeObject<List<User>>(jsonString);

public static DataTable ToDataTable<T>(this IList<T> data)
{
    PropertyDescriptorCollection props =
    TypeDescriptor.GetProperties(typeof(T));
    DataTable table = new DataTable();

    for(int i = 0 ; i < props.Count ; i++)
    {
        PropertyDescriptor prop = props[i];
        table.Columns.Add(prop.Name, prop.PropertyType);
    }
    object[] values = new object[props.Count];

    foreach (T item in data)
    {
        for (int i = 0; i < values.Length; i++)
        {
            values[i] = props[i].GetValue(item);
        }
        table.Rows.Add(values);
    }
    return table;        
}

but not sure where to go from there as the above just takes into account nrmal JSON data. Any help would be greatly appreciated.

1 Answer 1

4

Use this classes to deserialize data

         public class TitleDesc
    {
        public string Title { get; set; }
        public int Priority { get; set; }
        public Attribute[] attributes { get; set; }
    }

    public class Attribute
    {
        public string Title { get; set; }
        public string Value { get; set; }
        public int Priority { get; set; }
    }

then use this code to serialize

string jsonString = "[{'Title': 'OVERVIEW','Priority': 1,'attributes': [{    'Title': 'Type',    'Value': 'MacBook Pro',    'Priority': 1    },    {       'Title': 'Operating system',       'Value': 'OS X Mountain Lion',       'Priority': 2    },    {       'Title': 'Processor',       'Value': 'Intel Core i5 Processor (2.3 GHz, 3.3 GHz with TurboBoost, 6MB cache)',       'Priority': 3   },   {       'Title': 'Storage',       'Value': '500 GB HDDM 5400 rpm',       'Priority': 4   }]},{'Title': 'SPECIFICATION','Priority': 2,'attributes': [{    'Title': 'RAM',    'Value': '4 GB DDR3',    'Priority': 1    }]},{'Title': 'SCREEN','Priority': 3,'attributes': [{    'Title': 'Screen size',    'Value': '13',    'Priority': 1    }]}]";
        var data = JsonConvert.DeserializeObject<TitleDesc[]>(jsonString);

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes thats perfect! didnt think it would be that straight-forward, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.