我尝试了以下代码
private void Processjson(string jsonString)
{
JsonData jsonvale = JsonMapper.ToObject(jsonString);
Debug.Log (jsonvale["status"].ToJson()); // which prints "ok" on console
parseJSON parsejson;
string name = jsonvale["results"]["formatted_address"].ToString();
debug.log(name);
}
这里我不能打印名称(formatted_address)
我的json文件如下
{
"results" : [
{
"address_components" : [
{
"long_name" : "Udumalpet-Poosaripatti-Negamam Road",
"short_name" : "Udumalpet-Poosaripatti-Negamam Road",
"types" : [ "route" ]
},
{
"long_name" : "Seelakkampatti",
"short_name" : "Seelakkampatti",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Coimbatore",
"short_name" : "Coimbatore",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "642205",
"short_name" : "642205",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Udumalpet-Poosaripatti-Negamam Road, Seelakkampatti, Tamil Nadu 642205, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 10.6607381,
"lng" : 77.1360873
},
"southwest" : {
"lat" : 10.6576768,
"lng" : 77.1228984
}
},
"location" : {
"lat" : 10.6581087,
"lng" : 77.13010969999999
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 10.6607381,
"lng" : 77.1360873
},
"southwest" : {
"lat" : 10.6576768,
"lng" : 77.1228984
}
}
},
"place_id" : "ChIJ6SNTGUk0qDsRJs-tBBDJ6Ag",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "642205",
"short_name" : "642205",
"types" : [ "postal_code" ]
},
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Tamil Nadu 642205, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 10.698639,
"lng" : 77.26073269999999
},
"southwest" : {
"lat" : 10.6228222,
"lng" : 77.05489420000001
}
},
"location" : {
"lat" : 10.6583181,
"lng" : 77.1830243
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 10.698639,
"lng" : 77.26073269999999
},
"southwest" : {
"lat" : 10.6228222,
"lng" : 77.05489420000001
}
}
},
"place_id" : "ChIJ9en3D_s0qDsRIcoj9NdDJRg",
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "Coimbatore",
"short_name" : "Coimbatore",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Coimbatore, Tamil Nadu, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 11.4057969,
"lng" : 77.1766741
},
"southwest" : {
"lat" : 10.21881,
"lng" : 76.656036
}
},
"location" : {
"lat" : 10.9675289,
"lng" : 76.91820969999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 11.4057969,
"lng" : 77.1766741
},
"southwest" : {
"lat" : 10.21881,
"lng" : 76.656036
}
}
},
"place_id" : "ChIJtRyXL69ZqDsRl3v5ZMPoVUA",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Tamil Nadu, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 13.496666,
"lng" : 80.3464511
},
"southwest" : {
"lat" : 8.0690069,
"lng" : 76.23055409999999
}
},
"location" : {
"lat" : 11.1271225,
"lng" : 78.6568942
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 13.496666,
"lng" : 80.3464511
},
"southwest" : {
"lat" : 8.0774075,
"lng" : 76.23055409999999
}
}
},
"place_id" : "ChIJM5YYsYLFADsR8GEzRsx1lFU",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.5087008,
"lng" : 97.39535869999999
},
"southwest" : {
"lat" : 6.4626999,
"lng" : 68.1097
}
},
"location" : {
"lat" : 20.593684,
"lng" : 78.96288
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.5087008,
"lng" : 97.39498069999999
},
"southwest" : {
"lat" : 6.7535159,
"lng" : 68.16288519999999
}
}
},
"place_id" : "ChIJkbeSa_BfYzARphNChaFPjNc",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
我记录名称时的错误如下
InvalidOperationException: Instance of JsonData is not a dictionary
LitJson.JsonData.EnsureDictionary ()
如何记录字段[formatted_address]
答案 0 :(得分:1)
由于它是Unity,我们已经内置了JsonUtility。我刚刚复制了你的json字符串并在JsonUtils
下面的类中生成了public class AddressComponent
{
public string long_name;
public string short_name;
public List<string> types;
}
public class Northeast
{
public double lat;
public double lng;
}
public class Southwest
{
public double lat;
public double lng;
}
public class Bounds
{
public Northeast northeast;
public Southwest southwest;
}
public class Location
{
public double lat;
public double lng;
}
public class Viewport
{
public northeast;
public southwest;
}
public class Geometry
{
public Bounds bounds;
public Location location;
public string location_type;
public Viewport viewport;
}
public class Result
{
public List<AddressComponent> address_components;
public string formatted_address;
public Geometry geometry;
public string place_id;
public List<string> types;
}
public class Example
{
public List<Result> results;
public string status;
}
在单个脚本中添加上述类并将其添加到单元中。 现在尝试使用此代码来获取格式化的地址。
private void Processjson(string jsonString)
{
Example example = JsonUtility.FromJson<Example>(jsonString);
Debug.Log (example.results[0].formatted_address);
}
如果需要,您可以重命名示例类