通过索引访问json对象数据

时间:2018-08-03 11:10:39

标签: c# json node.js unity3d indexing

我有以下数组结构的json对象。我正在尝试从对象数组中检索某些元素

{
"data": [
{
"_id": "5b62dc6ebef986403db8aafd",
"name": "smitha vijaya",
"designation": "account management",
"projects": {
"project1": "description1",
"project2": "description2"
},
"age": "27"
},
{
"_id": "5b62dd17bef986403db8ab90",
"name": "JIKKU VARGHESE",
"designation": "SUPERVISING OPERATIONS MANAGER",
"projects": {
"project1": "description1",
"project2": "description2"
},
"age": "27"
},
{
"_id": "5b62dd76bef986403db8abe3",
"name": "SUJEETH  NAIR",
"designation": "MENA AMS",
"projects": {
"project1": "description1",
"project2": "description2"
},
"age": "30"
},
{
"_id": "5b62ddb1bef986403db8ac13",
"name": "GIRISH   KN",
"designation": "MENA  AMS",
"projects": {
"project1": "description1",
"project2": "description2"
},
"age": "27"
}
]
}

我正在使用以下c#代码提取ist名称(史密斯)

JsonData jsonvale = JsonMapper.ToObject( jsonString);
         Name = jsonvale["data"][0]["name"].ToString();
print (name);

我如何添加其他元素,例如名称jikku等?

3 个答案:

答案 0 :(得分:0)

您可以像下面这样遍历json数组:

JsonData jsonvale = JsonMapper.ToObject( jsonString);
for (var i=0; i<jsonvale.length; i++){
    print(jsonvale["data"][i]["name"].ToString());
}

答案 1 :(得分:0)

您可以使用for循环进行迭代:

for(int i = 0; i < jsonvale.length; i++) {
    JsonData jsonvale = JsonMapper.ToObject(jsonString);
    Name = jsonvale["data"][i]["name"].ToString();
    print(name);
}

答案 2 :(得分:0)

只需在List()中反序列化此json,然后您就可以访问此列表中的所有数据而无需循环,并且当您不再需要此列表时,可以销毁它。请参见以下示例https://github.com/IonCojucovschi/JsonDeserializeGenericForm