Unity 将 www.text 转换为字典以访问值



好的,我有这个可以尝试获取天气数据 - 这返回此字典的字符串版本:

Loaded following XML {"coord":{"lon":-118.24,"lat":34.05},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":290.19,"pressure":1027,"humidity":17,"temp_min":288.15,"temp_max":292.55},"visibility":16093,"wind":{"speed":1.27,"deg":20.0024},"clouds":{"all":1},"dt":1548269880,"sys":{"type":1,"id":3694,"message":0.0038,"country":"US","sunrise":1548255306,"sunset":1548292515},"id":5368361,"name":"Los Angeles","cod":200}

法典:

string url = "http://api.openweathermap.org/data/2.5/weather?lat=34.05&lon=-118.24&APPID=33710eba6d9c76286241d779ac1a6d9c";
        WWW www = new WWW(url);
        yield return www;
        if (www.error == null)
        {
            Debug.Log("Loaded following XML " + www.text);

我想在"天气"下获得描述,但不知道如何。选择单个节点不起作用:

print(xmlDoc.SelectSingleNode("cities/list/item/weather/description/@value").InnerText);

我能在这里做什么?

你得到的是一个JSON字符串,而不是一个XML

{
    "coord":{
        "lon":-118.24,
        "lat":34.05
    },
    "weather":[
        {
            "id":800,
            "main":"Clear",
            "description":"clear sky",
            "icon":"01d"
        }
    ],
    "base":"stations",
    "main":{
        "temp":290.19,
        "pressure":1027,
        "humidity":17,
        "temp_min":288.15,
        "temp_max":292.55
    },
    "visibility":16093,
    "wind":{
        "speed":1.27,
        "deg":20.0024
    },
    "clouds":{
        "all":1
    },
    "dt":1548269880,
    "sys":{
        "type":1,"id":3694,
        "message":0.0038,
        "country":"US",
        "sunrise":1548255306,
        "sunset":1548292515
    },
    "id":5368361,
    "name":"Los Angeles",
    "cod":200
} 

如果您只想访问该 JSON 的一个或几个值,您应该使用 SimpleJSON(只需将所有必需的脚本放在Assets中的某个位置)并执行类似操作

var N = JSON.Parse(www.text);
var weather = N["weather"];

并且由于weather是一个数组([...])访问单个值,例如

var id = weather[0]["id"];

但是要小心,因为这个 SimpleJson 通过简单地返回null而不是抛出异常来"隐藏"不正确的索引和字符串。这使得调试有时有点困难(但也可以在 JSON 类代码的代码中进行更改)。


还有例如 Unity 的JsonUtility但它要求您实现由 JSON 字符串表示的整个类。如果您不需要所有值,则在处理大型 JSON 时可能会产生大量开销。

但是,如果您需要它们(假设此处没有enum等简单类型):

[Serializable]
public class JsonData
{
    public Coord coord;
    public Weather[] weather;
    public string base;
    public Main main;
    public int visibility;
    public Wind wind;
    public Clouds clouds;
    public int dt;
    public Sys sys;
    public int id;
    public string name;
    public int cod;
}
[Serializable]
public class Coord
{
    public float lon;
    public float lat;
}
[Serializable]
public class Weather
{
    public int id;
    public string main;
    public string description;
    public string icon;
}
[Serializable]
public class Main
{
    public float temp;
    public int pressure;
    public int humidity;
    public float temp_min;
    public float temp_max;
}
[Serializable]
public class Wind 
{
    public float speed;
    public float deg;
}
[Serializable]
public class Clouds
{
    public int all;
}
[Serializable]
public class Sys
{
    public int type;
    public int id;
    public float message;
    public string country;
    public int sunrise;
    public int sunset;
}

比做

var wholeData = JsonUtility.FromJson<JsonData>(www.text);
var weather = wholeData.weather;
var id = weather.id;

最新更新