unity将www.text转换为字典以便访问值?

时间:2019-01-23 19:44:49

标签: c# json dictionary unity3d

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

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);

我在这里可以做什么?

1 个答案:

答案 0 :(得分:2)

您得到的是一个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's 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;