2014年5月14日星期三

java read json format data

{
    "status":0,
    "message":"ok",
    "results":[
        {
            "name":"重庆秦妈火锅(马仁山东路店)",
            "location":{
                "lat":31.312634,
                "lng":118.406372
            },
            "address":"弋江区马仁山东路275号(南瑞世纪联华超市向东200米)",
            "telephone":"0553-5919177",
            "uid":"d01b6bf9e9ea6f9e4d776e95",
            "detail_info":{
                "type":"cater",
                "tag":"火锅,餐饮",
                "detail_url":"http://api.map.baidu.com/place/detail?uid=d01b6bf9e9ea6f9e4d776e95&output=html&source=placeapi_v2",
                "price":"45",
                "overall_rating":4.5,
                "service_rating":2,
                "environment_rating":4.5,
                "image_num":"30",
                "groupon_num":4,
                "comment_num":"283"
            }
        }
    ]
}
json data in this format I've used java code form part of the data read out, but the data location and detail_info I know these two parts should be used JSONObject to read, but I do not know how to write , here is what I wrote to read other data in addition to location and detail_info two
    public static void main(String[] args) throws IOException {
        JSONArray jsonArray = searchLocation("芜湖");
        Iterator iteratorArray = jsonArray.iterator();
        while (iteratorArray.hasNext()) {
            JSONObject json = (JSONObject) iteratorArray.next();
            String results = json.getString("results");
            JSONArray resultArray = JSONArray.fromObject(results);
            Iterator iteratorResult = resultArray.iterator();
            while (iteratorResult.hasNext()) {
                JSONObject iteratorJson = (JSONObject) iteratorResult.next();
                System.out.println(iteratorJson.getString("name"));
                System.out.println(iteratorJson.getString("address"));
                System.out.println(iteratorJson.getString("telephone"));
                System.out.println(iteratorJson.getString("uid"));
                JSONObject json_location = (JSONObject) iteratorArray.next();
                JSONObject json_details = (JSONObject) iteratorArray.next();
                String location = json_location.getString("location");
                String details = json_details.getString("detail_info");
            }
        }
    }
next how to write it? Find Great God help !
------ Solution ---------------------------------------- ----
public static void main (String [] args) throws IOException {
JSONArray jsonArray = searchLocation (" Wuhu " ) ;
Iterator iteratorArray = jsonArray.iterator ();
while (iteratorArray.hasNext ()) {
JSONObject json = (JSONObject) iteratorArray.next ();
String results = json.getString ("results");
JSONArray resultArray = JSONArray.fromObject (results);
Iterator iteratorResult = resultArray.iterator ();
while (iteratorResult.hasNext ()) {
JSONObject iteratorJson = (JSONObject) iteratorResult. next ();
System.out.println (iteratorJson.getString ("name" ) ) ;
System.out.println (iteratorJson.getString ("address" ) ) ;
System.out.println (iteratorJson.getString ("telephone" ) ) ;
System.out.println (iteratorJson.getString ("uid" ) ) ;

String location = iteratorJson.getString ("location");
; String details = iteratorJson.getString ("detail_info");
JSONObject location = JSONObject.fromObject (location);
JSONObject details = JSONObject.fromObject (details);
System.out.println (details getString ("environment_rating").);

}
}
}
------ For reference only ------------------------- --------------
location and detail_info and on top of the same resolution, you can resolve part , I think the rest can be resolved .


------ For reference only ---------------------------------- -----

String location = json_location.getString ("location");
String details = json_details.getString ("detail_info");
JSONObject location = JSONObject.fromObject (location);
JSONObject details = JSONObject.fromObject (details);
details getString ("environment_rating");. would say take the money on the line.



JSONObject json string and javaBean is an intermediate object ,
------ For reference only ---------------- -----------------------
way upstairs to try it .
------ For reference only -------------------------------------- -
upstairs or not
------ For reference only ---------------------------- -----------
json if you do not have to turn into a java object such transfer , where you should write the wrong .
------ For reference only -------------------------------------- -
map there how to traverse the map on how to traverse

没有评论:

发表评论