Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从unity中的json数据中获取特定字段 使用谷歌反向地理编码,我得到了一个json 我需要从中访问特定字段_Json_Unity3d_Litjson - Fatal编程技术网

如何从unity中的json数据中获取特定字段 使用谷歌反向地理编码,我得到了一个json 我需要从中访问特定字段

如何从unity中的json数据中获取特定字段 使用谷歌反向地理编码,我得到了一个json 我需要从中访问特定字段,json,unity3d,litjson,Json,Unity3d,Litjson,我尝试了以下代码 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["resu

我尝试了以下代码

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);
}
这里我无法打印姓名(格式化的地址)

我的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]

因为它是Unity,我们已经有了内置的JsonUtility。我刚刚复制了您的json字符串,并从

公共类地址组件
{
公共字符串长名称;
公共字符串短名称;
公共列表类型;
}
公共课东北
{
公共双lat;
公共双液化天然气;
}
西南公务舱
{
公共双lat;
公共双液化天然气;
}
公共类界限
{
公共东北;
西南公共交通;
}
公共类位置
{
公共双lat;
公共双液化天然气;
}
公共类视口
{
公共东北;
公共西南部;
}
公共课几何
{
公共边界;
公共场所;
公共字符串位置类型;
公共视口;
}
公开课成绩
{
公共地址列表组件;
公共字符串格式的地址;
公共几何;
公共字符串位置标识;
公共列表类型;
}
公开课范例
{
公开名单结果;
公共字符串状态;
}
在单个脚本中添加上述类并将其添加到unity中。 现在,请尝试使用此代码获取格式化的地址

private void Processjson(string jsonString)
{
    Example example = JsonUtility.FromJson<Example>(jsonString);

    Debug.Log (example.results[0].formatted_address);
}
private void Processjson(字符串jsonString)
{
示例=JsonUtility.FromJson(jsonString);
Debug.Log(示例.results[0]。格式化的\u地址);
}

如果您想

debug.log(“name”)将打印文本名称,则可以重命名示例类。如果您想打印变量的内容,则必须使用该类而不带引号:debug.log(name);对不起,我是说name@RafalZiolkowski我编辑了这个问题看起来结果包含数组,你应该试试jsonvale[“results”][0][“formatted_address”]好的,我不知道这个json库,但请检查以下答案:编辑后的属性替换为字段,IList替换为列表,因为Unity不会序列化这些。你能共享你的邮件id吗。。我得到评论后会删除它
private void Processjson(string jsonString)
{
    Example example = JsonUtility.FromJson<Example>(jsonString);

    Debug.Log (example.results[0].formatted_address);
}