Reputation: 688
Hi I have a little problem with deseralizing JSON.
It's json which i get from API
"{\"status\":\"ok\",\"categories\":
[{\"id\":1,\"name\":\"Name\",\"number\":0,\"clientsCount\":32,\"fields\":
[\"name\",\"surname\",\"tel\",\"post\",\"country\"],\"subcategories\":[{\"id\":2,\"name\":\"Got\",\"number\":1},{\"id\":13,\"name\":\"Hipoteka\",\"number\":2},
{\"id\":14,\"name\":\"Samochodowych\",\"number\":4}]},
{\"id\":2,\"name\":\"Name\",\"number\":1,\"clientsCount\":12,\"fields\":
[\"name\",\"nazwisko\",\"tel\",\"car\",\"car_model\"],\"subcategories\":[]}]}"
and this is how it looks like in PHP
array(
'id' => 1,
'name' => 'Name',
'number' => 0,
'clientsCount' => 32,
'fields' => array(
'name',
'surname',
'tel',
'post',
'country'
),
'subcategories' => array(
array(
'id' => 2,
'name' => 'Gotówkowy',
'number' => 1
),
array(
'id' => 13,
'name' => 'Hipoteka',
'number' => 2
),
array(
'id' => 14,
'name' => 'Samochodowych',
'number' => 4
),
),
),
array(
'id' => 2,
'name' => 'Name',
'number' => 1,
'clientsCount' => 12,
'fields' => array(
'name',
'surname',
'tel',
'car',
'car_model'
),
'subcategories' => array(),
),
I don't have any idea how to convert it. I dont need from this JSON fields subcategories but others are very important for me.
I have prepared some class but I do not know How to load to this class array of 'fields'
class Kategorie
{
public int id { get; set; }
public string name { get; set; }
public int number { get; set; }
public int clientsCount { get; set; }
}
Upvotes: 0
Views: 202
Reputation: 3502
Here is a sample based on the Json you posted:
//Created MyObject to resemble the JSON object graph for easy deserialization
class MyObject
{
public string Status { get; set; }
public Category[] Categories { get; set; }
//Similar to categories, You can create properties for fields, subcategories etc., as needed.
}
class Category
{
public int id { get; set; }
public string name { get; set; }
public int number { get; set; }
public int clientsCount { get; set; }
}
Deserialization Code: (Using Newtonsoft.Json)
var myObject = JsonConvert.DeserializeObject<MyObject>(jsonString);
Hope this help.
Upvotes: 4