What you get there is a JSON string not an 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
}
If you want to access only one or some few values of that JSON you should use the SimpleJSON (simply place all required scripts somewhere in your Assets
) and do something like
var N = JSON.Parse(www.text);
var weather = N["weather"];
and than since weather
is an array ([...]
) access the single values like e.g.
var id = weather[0]["id"];
Carefull however because this SimpleJson "hides" incorrect indexes and strings by simply returning null
instead of throwing an exception. This makes debugging a bit harder sometimes (but could also be changed within the code of the JSON class code).
There is also e.g. Unity's JsonUtility
but it requires that you implement the entire class which is represented by the JSON string. If you do not need all values this might be a lot of overhead when dealing with huge JSONs.
If you need them however (assuming simple types here no enum
etc):
[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;
}
and than do
var wholeData = JsonUtility.FromJson<JsonData>(www.text);
var weather = wholeData.weather;
var id = weather.id;