如何解析这个json数据到java [英] how to parse this json data to java

查看:112
本文介绍了如何解析这个json数据到java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是json代码..我是json解析的新手..所以请让我知道如何将这个json数据解析为java。
在此先感谢!

  {
几何体:{
location :{
lat:13.0561990,
lng:80.2348820
}
},
icon:http://maps.gstatic.com /mapfiles/place_api/icons/generic_business-71.png
标识: cf67cea9c203824f58eaf5abe98a08912593bf90,
名: Mahalingapuram分公司,
参考: CnRsAAAAlAu76wP4EspUn8qk6yUBndsDI7RaEmDaXHc2gP1bomAP_22ZeswWfj3ganEwX9jO2BYjRo6eoeKlwrSeMNDv5h94zqnsNncDntlKN_MqC-
类型:[建立],
附近:钦奈Nungambakkam Mahalingapuram大道










$ b $在这里解析java代码后发生错误

 $ c> 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 adjacent = 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);
}


解决方案

<我使用以下内容:



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



序列化为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


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

解决方案

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

这篇关于如何解析这个json数据到java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆