简体   繁体   English

使用 SimpleJSON 从 Web API 解析 Unity 中的 JSON 数据数组

[英]Parsing JSON data array in Unity from a web API using SimpleJSON

Hi thanks for taking time to look at my issue.嗨,感谢您花时间查看我的问题。 I am using SimpleJSON to parse a stream of data coming from https://api.thingspeak.com/channels/1327025/fields/1.json?results=1 specifically trying to get to "field1" which is inside "feeds".我正在使用 SimpleJSON 来解析来自https://api.thingspeak.com/channels/1327025/fields/1.json?results=1的数据流,专门试图到达“feeds”内的“field1”。

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

public class onLoad : MonoBehaviour
{
    public TMP_Text temperatureData;

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

public class onLoad : MonoBehaviour
{
    public TMP_Text temperatureData;

    public void GetJsonData()
    {
        StartCoroutine(RequestWebService());
        Debug.Log("test");
    }

    IEnumerator RequestWebService()
    {
        string jsonString = "https://api.thingspeak.com/channels/1327025/fields/1.json?results=1";
        Debug.Log(jsonString);

        using (UnityWebRequest webData = UnityWebRequest.Get(jsonString))
        {
            yield return webData.SendWebRequest();
            if (webData.isNetworkError || webData.isHttpError)
            {
                print("---------------- ERROR ----------------");
                print(webData.error);
            }
            else
            {
                if (webData.isDone)
                {
                    JSONNode jsonData = JSON.Parse(System.Text.Encoding.UTF8.GetString(webData.downloadHandler.data));

                    if (jsonData == null)
                    {
                        print("---------------- NO DATA ----------------");
                    }
                    else
                    {
                        print("---------------- JSON DATA ----------------");
                        print("jsonData.Count:" + jsonData.Count);

                        temperatureData.text = jsonData["feeds\field1"];
                        Debug.Log(temperatureData.text);
        }
    }
            }}}}

My code seems to work fine, as in I get the "jsonData.Count" to work (which comes up as 2).我的代码似乎工作正常,因为我让“jsonData.Count”工作(出现为 2)。 I am assuming that its reading the "channel" and "feeds".我假设它正在阅读“频道”和“提要”。 But it pops out as null.但它弹出为空。

How do I place the key and item inside jsonData to get to field1 ?如何将密钥和项目放入 jsonData 以到达 field1 ? Thanks谢谢

The simple JSON basically works like a dictionary with single keywords ... you can't just pass a path to it.简单的 JSON 基本上就像一个带有单个关键字的字典......你不能只传递一个路径给它。

What you want is你想要的是

  • first accessing the feeds首先访问feeds
  • it is an array so get an element .. probably the first one?它是一个数组,所以获取一个元素 .. 可能是第一个?
  • of that element finally access the field1该元素的最终访问field1

So something like所以像

temperatureData.text = jsonData["feeds"][0]["field1"].Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM