嗨我在解除JSON设置方面遇到了一些问题。
我从API获得的json
"{\"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\":[]}]}"
这就是它在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(),
),
我不知道如何转换它。我不需要从这个JSON字段子类别,但其他对我来说非常重要。
我已经准备了一些课程,但我不知道如何加载到这个字段数组'
class Kategorie
{
public int id { get; set; }
public string name { get; set; }
public int number { get; set; }
public int clientsCount { get; set; }
}
答案 0 :(得分:4)
以下是基于您发布的Json的示例:
//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; }
}
反序列化代码:(使用Newtonsoft.Json)
var myObject = JsonConvert.DeserializeObject<MyObject>(jsonString);
希望得到这个帮助。