如何将这个json数据解析为java

发布于 2024-12-07 14:58:34 字数 1813 浏览 3 评论 0原文

这是json代码..我是json解析的新手..所以请让我知道如何将此json数据解析为java。 提前致谢!!

  {
     "geometry" : {
        "location" : {
           "lat" : 13.0561990,
           "lng" : 80.2348820
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
     "id" : "cf67cea9c203824f58eaf5abe98a08912593bf90",
     "name" : "Mahalingapuram Branch",
     "reference" : "CnRsAAAAlAu76wP4EspUn8qk6yUBndsDI7RaEmDaXHc2gP1bomAP_22ZeswWfj3ganEwX9jO2BYjRo6eoeKlwrSeMNDv5h94zqnsNncDntlKN_MqC-gegTBZLX5YxOSA-vzuk6bKe3BJlHytW_wKJJs0Nxf3jRIQT1yYWTlDdMhk9EyS8ulMghoUZK5J51RRVKo12LPdHm_qCQWX_VA",
     "types" : [ "establishment" ],
     "vicinity" : "Mahalingapuram Main Road, Nungambakkam, Chennai"
  }

解析这里的java代码后但出现错误

public static void main(String[] args) throws Exception{
    InputStream is = JsonParsing.class.getResourceAsStream( "");
    String jsonTxt = IOUtils.toString( is );
    JSONObject json = (JSONObject)JSONSerializer.toJSON(jsonTxt);   
    JSONObject html_attribution = json.getJSONObject(“html_attribution");
    JSONObject geometry= json.getJSONObject(“geometry”);
    JSONObject location= json.getJSONObject(“location”);

    double latitude = json.getDouble("latitude");
    double longitude = json.getDouble("longitude");
    String icon= result.getString("icon");
    String id = result.getString("id");
    String reference= result.getString("reference");
    String name = result.getString("name");
    String vicinity = result.getstring("vicinity");

    System.out.println("html_attribution: " + html_attribution);
    System.out.println("location: " + location);
    System.out.println(“latitude: “ + latitude);
    System.out.println(“latitude: “ + longitude);
    System.out.println("result: " + result);
}

Here is the json code..i am new to json parsing..so please let me know how to parse this json data to java.
thanks in advance!!

  {
     "geometry" : {
        "location" : {
           "lat" : 13.0561990,
           "lng" : 80.2348820
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
     "id" : "cf67cea9c203824f58eaf5abe98a08912593bf90",
     "name" : "Mahalingapuram Branch",
     "reference" : "CnRsAAAAlAu76wP4EspUn8qk6yUBndsDI7RaEmDaXHc2gP1bomAP_22ZeswWfj3ganEwX9jO2BYjRo6eoeKlwrSeMNDv5h94zqnsNncDntlKN_MqC-gegTBZLX5YxOSA-vzuk6bKe3BJlHytW_wKJJs0Nxf3jRIQT1yYWTlDdMhk9EyS8ulMghoUZK5J51RRVKo12LPdHm_qCQWX_VA",
     "types" : [ "establishment" ],
     "vicinity" : "Mahalingapuram Main Road, Nungambakkam, Chennai"
  }

after parsing the java code here but error occur

public static void main(String[] args) throws Exception{
    InputStream is = JsonParsing.class.getResourceAsStream( "");
    String jsonTxt = IOUtils.toString( is );
    JSONObject json = (JSONObject)JSONSerializer.toJSON(jsonTxt);   
    JSONObject html_attribution = json.getJSONObject(“html_attribution");
    JSONObject geometry= json.getJSONObject(“geometry”);
    JSONObject location= json.getJSONObject(“location”);

    double latitude = json.getDouble("latitude");
    double longitude = json.getDouble("longitude");
    String icon= result.getString("icon");
    String id = result.getString("id");
    String reference= result.getString("reference");
    String name = result.getString("name");
    String vicinity = result.getstring("vicinity");

    System.out.println("html_attribution: " + html_attribution);
    System.out.println("location: " + location);
    System.out.println(“latitude: “ + latitude);
    System.out.println(“latitude: “ + longitude);
    System.out.println("result: " + result);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二智少女猫性小仙女 2024-12-14 14:58:34

我使用以下内容:

http://code.google.com/p/google-gson/< /a>

序列化为 JSON:

Gson gson = new GsonBuilder().create(); 
String jsonOutput = gson.toJson(someObject);

从 JSON 反序列化:

BagOfPrimitives obj = gson.fromJson(json, BagOfPrimitives.class);

可以在此处找到用户指南:

https://sites.google.com/site/gson/gson-user-guide

I use the following:

http://code.google.com/p/google-gson/

Serialize to JSON:

Gson gson = new GsonBuilder().create(); 
String jsonOutput = gson.toJson(someObject);

De-serialize from JSON:

BagOfPrimitives obj = gson.fromJson(json, BagOfPrimitives.class);

A user guide can be found here:

https://sites.google.com/site/gson/gson-user-guide

椵侞 2024-12-14 14:58:34

使用 http://www.json.org/ 中的 java 库。

Use a java library from http://www.json.org/.

深陷 2024-12-14 14:58:34

查看 GSON Google 项目。它是一个库,可让您将所有 JSON 数据转换为 Java 对象。这很简单。

Take a look at the GSON Google project. Its a library that lets you get all your JSON data into Java objects. Its quite simple.

朮生 2024-12-14 14:58:34
JSONObject geometry = json.getJSONObject("geometry");

JSONObject location = json.getJSONObject("location");

String lat = location.getString("lat");
String lng = location.getString("lng");

icon = json.getString("icon");

id = json.getString("id");

name = json.getString("name");

reference = json.getString("reference");

vicinity = json.getString("vicinity");

JSOnArray types = json.getJSONArray("types");

for (int i=0;i<=types.length();i++)
{

JSonObject temJson = (JSONObject) types.get(i);

String Varialb = temJson.get(i);

}
JSONObject geometry = json.getJSONObject("geometry");

JSONObject location = json.getJSONObject("location");

String lat = location.getString("lat");
String lng = location.getString("lng");

icon = json.getString("icon");

id = json.getString("id");

name = json.getString("name");

reference = json.getString("reference");

vicinity = json.getString("vicinity");

JSOnArray types = json.getJSONArray("types");

for (int i=0;i<=types.length();i++)
{

JSonObject temJson = (JSONObject) types.get(i);

String Varialb = temJson.get(i);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文