使用SimpleJSON从JSONNode 2d数组中提取数组

时间:2019-02-25 11:20:00

标签: arrays json simplejson

我从服务器获取以下JSON

{
    "UniqueModelURL": "https://s3-us-west-2.amazonaws.com/ticomsoft-image-repo",
    "Images": [{
        "Cat1": [{
            "Img1": "1.png"
        }, {
            "Img2": "2.png"
        }, {
            "Img3": "3.png"
        }, {
            "Img4": "4.png"
        }]
    }]
}

使用UnityWebRequest成功解析JSON之后,我将字符串化的json移至void ProcessJSON(string jsonString)

void ProcessJSON(string jsonString)
    {
        JSONNode JNode = JSON.Parse(jsonString);
        var prefix = JNode["UniqueModelURL"].Value;

        Debug.Log(prefix + " of type "+ prefix.GetType()); //returnd the UniqueModelURL;

        JSONArray Images = JNode["Images"].AsArray;

        Debug.Log(Images.Count + " of type "+ Images.GetType());

    }

现在我的问题是从Images数组中提取Cat1(可能超过1个类别)

我尝试过

JSONArray cat1= JNode["Images"][1].AsArray; 然后我打印了cat1.Count,它为0,所以出了点问题。 图片。计数为1;

JSONArray cat1 = JNode["Images"]["Cat1"].AsArray; 与上述结果相同;

JSONArray Cat1 = Images.Values[0];

JSONArray cat1 = Images.getJSONObject(0).AsArray; 得到(错误CS1061:“ JSONArray”不包含“ getJSONObject”的定义)

奇怪的是,当我遵循SimpleJSON Wiki

的语法时
var prefix = JNode["UniqueModelURL"].Value;
Debug.Log(prefix + " of type "+ prefix.GetType());

产生了“ https://s3-us-west-2.amazonaws.com/ticomsoft-image-repo”; 但是

var str = JNode["Images"]["Cat1"][1]["Img2"].Value;
Debug.Log(str + " of type "+ str.GetType());

产生空字符串

为了记录,其余代码为:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;
using UnityEngine.Networking;

public class ParseJSONFromServer : MonoBehaviour
{

    public string JSON_URL;
    // Start is called before the first frame update
    IEnumerator Start()
    {
        Debug.Log("start");
        UnityWebRequest www = UnityWebRequest.Get(JSON_URL);
        yield return www.SendWebRequest();

        if (www.error == null)
         {
             Debug.Log("success: "+ Time.time);
             ProcessJSON(www.downloadHandler.text);
         }
         else
         {
             Debug.Log("ERROR: " + www.error);
         } 
    }

0 个答案:

没有答案