【问题标题】:Unity convert www.text to dictionary in order to access value?Unity将www.text转换为字典以访问值?
【发布时间】:2019-01-23 19:44:48
【问题描述】:

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

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

我可以在这里做什么?

【问题讨论】:

标签: c# json dictionary unity3d


【解决方案1】:

你得到的是一个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;

【讨论】:

  • 当使用JsonUtility 时,我很确定您只需要实现您想要访问的字段并且忽略未映射的 JSON。不是这样吗? (诚​​然,我只用过FromJsonOverwrite()
  • @Immersive 我在文档中没有找到这个。它只声称与 If a field of the object is not present in the JSON representation, that field will be left unchanged. 相反,但它没有提及 JSON 中存在但对象中不存在的字段。
猜你喜欢
  • 2022-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
相关资源
最近更新 更多