无法在我的java程序中解析json

时间:2016-02-08 06:14:57

标签: java json

我正在尝试将json对象解码为多个字符串。我不知道如何解析json对象。

这是我的json字符串

{"page_1":"{\"city\":\"Bangalore\",\"locality\":\"Battarahalli\",\"Name_of_Person\":\"xxx\",\"User_email_address\":\"xxx@gmail.com\",\"user_phone_number\":\"\",\"sub_locality\":\"\",\"street_name\":\"7th Cross Road, Near Reliance Fresh, T.c Palya,\",\"home_plot_no\":\"45\",\"pin_code\":\"560049\",\"project_society_build_name\":\"Sunshine Layout\",\"landmark_reference_1\":\"\",\"landmark_reference_2\":\"\",\"No_of_Schools\":20,\"No_of_Hospitals\":20,\"No_of_Metro\":0,\"No_of_Mall\":11,\"No_of_Park\":10,\"Distance_of_schools\":1.55,\"Distance_of_Hospitals\":2.29,\"Distance_of_Metro\":0,\"Distance_of_Mall\":1.55,\"Distance_of_Park\":2.01,\"lat\":13.0243273,\"lng\":77.7077906,\"ipinfo\":{\"ip\":\"113.193.30.130\",\"hostname\":\"No Hostname\",\"city\":\"\",\"region\":\"\",\"country\":\"IN\",\"loc\":\"20.0000,77.0000\",\"org\":\"AS45528 Tikona Digital Networks Pvt Ltd.\"}}","page_2":"{\"home_type\":\"Flat\",\"area\":\"1350\",\"beds\":\"3 BHK\",\"bath_rooms\":2,\"building_age\":\"1\",\"floors\":2,\"balcony\":2,\"amenities\":\"premium\",\"amenities_options\":{\"gated_security\":\"\",\"physical_security\":\"\",\"cctv_camera\":\"\",\"controll_access\":\"\",\"elevator\":true,\"power_back_up\":\"\",\"parking\":true,\"partial_parking\":\"\",\"onsite_maintenance_store\":\"\",\"open_garden\":\"\",\"party_lawn\":\"\",\"amenities_balcony\":\"\",\"club_house\":\"\",\"fitness_center\":\"\",\"swimming_pool\":\"\",\"party_hall\":\"\",\"tennis_court\":\"\",\"basket_ball_court\":\"\",\"squash_coutry\":\"\",\"amphi_theatre\":\"\",\"business_center\":\"\",\"jogging_track\":\"\",\"convinience_store\":\"\",\"guest_rooms\":\"\"},\"interior\":\"regular\",\"interior_options\":{\"tiles\":true,\"marble\":\"\",\"wooden\":\"\",\"modular_kitchen\":\"\",\"partial_modular_kitchen\":\"\",\"gas_pipe\":\"\",\"intercom_system\":\"\",\"air_conditioning\":\"\",\"partial_air_conditioning\":\"\",\"wardrobe\":\"\",\"sanitation_fixtures\":\"\",\"false_ceiling\":\"\",\"partial_false_ceiling\":\"\",\"recessed_lighting\":\"\"},\"location\":\"regular\",\"location_options\":{\"good_view\":true,\"transporation_hub\":true,\"shopping_center\":\"\",\"hospital\":\"\",\"school\":\"\",\"ample_parking\":\"\",\"park\":\"\",\"temple\":\"\",\"bank\":\"\",\"less_congestion\":\"\",\"less_pollution\":\"\"},\"maintenance\":\"\",\"maintenance_value\":\"\",\"near_by\":{\"school\":\"\",\"hospital\":\"\",\"mall\":\"\",\"park\":\"\",\"metro\":\"\",\"Near_by_school\":\"Little Champ Gurukulam Pre School \\\/ 1.52 km\",\"Near_by_hospital\":\"Suresh Hospital \\\/ 2.16 km\",\"Near_by_mall\":\"LORVEN LEO \\\/ 2.13 km\",\"Near_by_park\":\"SURYA ENCLAIVE \\\/ 2.09 km\"},\"city\":\"Bangalore\",\"locality\":\"Battarahalli\",\"token\":\"344bd4f0fab99b460873cfff6befb12f\"}"}

我试过这个

String JSON = "{\"page_1\":{\"city\":\"Bangalore\",\"locality\":\"Anekal\",\"Name_of_Person\":\"sas\",\"User_email_address\":\"ddd32@gmail.com\",\"user_phone_number\":\"\",\"sub_locality\":\"\",\"street_name\":\"farahana\",\"home_plot_no\":\"50\",\"pin_code\":\"\",\"project_society_build_name\":\"\",\"landmark_reference_1\":\"\",\"landmark_reference_2\":\"\",\"No_of_Schools\":20,\"No_of_Hospitals\":8,\"No_of_Metro\":1,\"No_of_Mall\":1,\"No_of_Park\":0,\"Distance_of_schools\":2.51,\"Distance_of_Hospitals\":0.43,\"Distance_of_Metro\":2.55,\"Distance_of_Mall\":0.89,\"Distance_of_Park\":0,\"lat\":12.7105259,\"lng\":77.6911295,\"ipinfo\":{\"ip\":\"113.193.30.130\",\"hostname\":\"No Hostname\",\"city\":\"\",\"region\":\"\",\"country\":\"IN\",\"loc\":\"20.0000,77.0000\",\"org\":\"AS45528 Tikona Digital Networks Pvt Ltd.\"}}\n
JSONObject obj = new JSONObject(JSON);
String city =  (String) obj.get("city");
System.out.println(city);

但它只会抛出错误

Expected a ',' or '}' at 711 [character 1 line 2]

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

你使用这个JSON。因为你留下了一个紧密的花括号:

{
    "page_1": {
        "city": "Bangalore",
        "locality": "Anekal",
        "Name_of_Person": "sas",
        "User_email_address": "ddd32@gmail.com",
        "user_phone_number": "",
        "sub_locality": "",
        "street_name": "farahana",
        "home_plot_no": "50",
        "pin_code": "",
        "project_society_build_name": "",
        "landmark_reference_1": "",
        "landmark_reference_2": "",
        "No_of_Schools": 20,
        "No_of_Hospitals": 8,
        "No_of_Metro": 1,
        "No_of_Mall": 1,
        "No_of_Park": 0,
        "Distance_of_schools": 2.51,
        "Distance_of_Hospitals": 0.43,
        "Distance_of_Metro": 2.55,
        "Distance_of_Mall": 0.89,
        "Distance_of_Park": 0,
        "lat": 12.7105259,
        "lng": 77.6911295,
        "ipinfo": {
            "ip": "113.193.30.130",
            "hostname": "No Hostname",
            "city": "",
            "region": "",
            "country": "IN",
            "loc": "20.0000,77.0000",
            "org": "AS45528 Tikona Digital Networks Pvt Ltd."
        }
    }
}

你使用json pro lint调试你的JSON:链接是 http://pro.jsonlint.com/

public class Kunfu {

    public static void main(String a[]) throws JSONException{

        String JSON = "{\"page_1\":{\"city\":\"Bangalore\",\"locality\":\"Anekal\",\"Name_of_Person\":\"sas\",\"User_email_address\":\"ddd32@gmail.com\",\"user_phone_number\":\"\",\"sub_locality\":\"\",\"street_name\":\"farahana\",\"home_plot_no\":\"50\",\"pin_code\":\"\",\"project_society_build_name\":\"\",\"landmark_reference_1\":\"\",\"landmark_reference_2\":\"\",\"No_of_Schools\":20,\"No_of_Hospitals\":8,\"No_of_Metro\":1,\"No_of_Mall\":1,\"No_of_Park\":0,\"Distance_of_schools\":2.51,\"Distance_of_Hospitals\":0.43,\"Distance_of_Metro\":2.55,\"Distance_of_Mall\":0.89,\"Distance_of_Park\":0,\"lat\":12.7105259,\"lng\":77.6911295,\"ipinfo\":{\"ip\":\"113.193.30.130\",\"hostname\":\"No Hostname\",\"city\":\"\",\"region\":\"\",\"country\":\"IN\",\"loc\":\"20.0000,77.0000\",\"org\":\"AS45528 Tikona Digital Networks Pvt Ltd.\"}}}\n";
                JSONObject obj = new JSONObject(JSON);
                String city =  (String) obj.getJSONObject("page_1").get("city");
                System.out.println(city);
}}

<强>输出:

Bangalore

此代码运行良好。你尝试它,你添加JSON jar jar文件链接是:  的 http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm

答案 1 :(得分:0)

不能在评论中使用此功能,但会尝试做空:

首先,你得到Expected a ',' or '}' at 711 [character 1 line 2],因为你刚刚复制了初始json的前半部分。您的初始Json看起来像这样:{"page_1": "...", "page_2": "..."},但代码中的json看起来像{"page_1": {...},而您却错过了关闭}

如果你真的需要你的代码中的json,你必须这样做 1)在String JSON的末尾添加结束} 2)先取page_1个对象,然后从中取city个字符串。这是演示:

String JSON = "{\"page_1\":{\"city\":\"Bangalore\",\"locality\":\"Anekal\",\"Name_of_Person\":\"sas\",\"User_email_address\":\"ddd32@gmail.com\",\"user_phone_number\":\"\",\"sub_locality\":\"\",\"street_name\":\"farahana\",\"home_plot_no\":\"50\",\"pin_code\":\"\",\"project_society_build_name\":\"\",\"landmark_reference_1\":\"\",\"landmark_reference_2\":\"\",\"No_of_Schools\":20,\"No_of_Hospitals\":8,\"No_of_Metro\":1,\"No_of_Mall\":1,\"No_of_Park\":0,\"Distance_of_schools\":2.51,\"Distance_of_Hospitals\":0.43,\"Distance_of_Metro\":2.55,\"Distance_of_Mall\":0.89,\"Distance_of_Park\":0,\"lat\":12.7105259,\"lng\":77.6911295,\"ipinfo\":{\"ip\":\"113.193.30.130\",\"hostname\":\"No Hostname\",\"city\":\"\",\"region\":\"\",\"country\":\"IN\",\"loc\":\"20.0000,77.0000\",\"org\":\"AS45528 Tikona Digital Networks Pvt Ltd.\"}}}";
try {
    JSONObject obj = new JSONObject(JSON);
    JSONObject page_1 = obj.getJSONObject("page_1");
    String city = page_1.getString("city");
    System.out.println(city);
} catch (JSONException e) {
    e.printStackTrace();
}

但是您的第一个字符串中的Json与您的代码中的Json不同。您的初始Json具有以下格式:{"page_1": "...", "page_2": "..."}并且如您所见,有值而不是对象的字符串。所以它有点不同,这是演示:

String JSON = "{\"page_1\":\"{\\\"city\\\":\\\"Bangalore\\\",\\\"locality\\\":\\\"Battarahalli\\\",\\\"Name_of_Person\\\":\\\"xxx\\\",\\\"User_email_address\\\":\\\"xxx@gmail.com\\\",\\\"user_phone_number\\\":\\\"\\\",\\\"sub_locality\\\":\\\"\\\",\\\"street_name\\\":\\\"7th Cross Road, Near Reliance Fresh, T.c Palya,\\\",\\\"home_plot_no\\\":\\\"45\\\",\\\"pin_code\\\":\\\"560049\\\",\\\"project_society_build_name\\\":\\\"Sunshine Layout\\\",\\\"landmark_reference_1\\\":\\\"\\\",\\\"landmark_reference_2\\\":\\\"\\\",\\\"No_of_Schools\\\":20,\\\"No_of_Hospitals\\\":20,\\\"No_of_Metro\\\":0,\\\"No_of_Mall\\\":11,\\\"No_of_Park\\\":10,\\\"Distance_of_schools\\\":1.55,\\\"Distance_of_Hospitals\\\":2.29,\\\"Distance_of_Metro\\\":0,\\\"Distance_of_Mall\\\":1.55,\\\"Distance_of_Park\\\":2.01,\\\"lat\\\":13.0243273,\\\"lng\\\":77.7077906,\\\"ipinfo\\\":{\\\"ip\\\":\\\"113.193.30.130\\\",\\\"hostname\\\":\\\"No Hostname\\\",\\\"city\\\":\\\"\\\",\\\"region\\\":\\\"\\\",\\\"country\\\":\\\"IN\\\",\\\"loc\\\":\\\"20.0000,77.0000\\\",\\\"org\\\":\\\"AS45528 Tikona Digital Networks Pvt Ltd.\\\"}}\",\"page_2\":\"{\\\"home_type\\\":\\\"Flat\\\",\\\"area\\\":\\\"1350\\\",\\\"beds\\\":\\\"3 BHK\\\",\\\"bath_rooms\\\":2,\\\"building_age\\\":\\\"1\\\",\\\"floors\\\":2,\\\"balcony\\\":2,\\\"amenities\\\":\\\"premium\\\",\\\"amenities_options\\\":{\\\"gated_security\\\":\\\"\\\",\\\"physical_security\\\":\\\"\\\",\\\"cctv_camera\\\":\\\"\\\",\\\"controll_access\\\":\\\"\\\",\\\"elevator\\\":true,\\\"power_back_up\\\":\\\"\\\",\\\"parking\\\":true,\\\"partial_parking\\\":\\\"\\\",\\\"onsite_maintenance_store\\\":\\\"\\\",\\\"open_garden\\\":\\\"\\\",\\\"party_lawn\\\":\\\"\\\",\\\"amenities_balcony\\\":\\\"\\\",\\\"club_house\\\":\\\"\\\",\\\"fitness_center\\\":\\\"\\\",\\\"swimming_pool\\\":\\\"\\\",\\\"party_hall\\\":\\\"\\\",\\\"tennis_court\\\":\\\"\\\",\\\"basket_ball_court\\\":\\\"\\\",\\\"squash_coutry\\\":\\\"\\\",\\\"amphi_theatre\\\":\\\"\\\",\\\"business_center\\\":\\\"\\\",\\\"jogging_track\\\":\\\"\\\",\\\"convinience_store\\\":\\\"\\\",\\\"guest_rooms\\\":\\\"\\\"},\\\"interior\\\":\\\"regular\\\",\\\"interior_options\\\":{\\\"tiles\\\":true,\\\"marble\\\":\\\"\\\",\\\"wooden\\\":\\\"\\\",\\\"modular_kitchen\\\":\\\"\\\",\\\"partial_modular_kitchen\\\":\\\"\\\",\\\"gas_pipe\\\":\\\"\\\",\\\"intercom_system\\\":\\\"\\\",\\\"air_conditioning\\\":\\\"\\\",\\\"partial_air_conditioning\\\":\\\"\\\",\\\"wardrobe\\\":\\\"\\\",\\\"sanitation_fixtures\\\":\\\"\\\",\\\"false_ceiling\\\":\\\"\\\",\\\"partial_false_ceiling\\\":\\\"\\\",\\\"recessed_lighting\\\":\\\"\\\"},\\\"location\\\":\\\"regular\\\",\\\"location_options\\\":{\\\"good_view\\\":true,\\\"transporation_hub\\\":true,\\\"shopping_center\\\":\\\"\\\",\\\"hospital\\\":\\\"\\\",\\\"school\\\":\\\"\\\",\\\"ample_parking\\\":\\\"\\\",\\\"park\\\":\\\"\\\",\\\"temple\\\":\\\"\\\",\\\"bank\\\":\\\"\\\",\\\"less_congestion\\\":\\\"\\\",\\\"less_pollution\\\":\\\"\\\"},\\\"maintenance\\\":\\\"\\\",\\\"maintenance_value\\\":\\\"\\\",\\\"near_by\\\":{\\\"school\\\":\\\"\\\",\\\"hospital\\\":\\\"\\\",\\\"mall\\\":\\\"\\\",\\\"park\\\":\\\"\\\",\\\"metro\\\":\\\"\\\",\\\"Near_by_school\\\":\\\"Little Champ Gurukulam Pre School \\\\\\/ 1.52 km\\\",\\\"Near_by_hospital\\\":\\\"Suresh Hospital \\\\\\/ 2.16 km\\\",\\\"Near_by_mall\\\":\\\"LORVEN LEO \\\\\\/ 2.13 km\\\",\\\"Near_by_park\\\":\\\"SURYA ENCLAIVE \\\\\\/ 2.09 km\\\"},\\\"city\\\":\\\"Bangalore\\\",\\\"locality\\\":\\\"Battarahalli\\\",\\\"token\\\":\\\"344bd4f0fab99b460873cfff6befb12f\\\"}\"}";

try {
    JSONObject obj = new JSONObject(JSON);
    String page_1Str = obj.getString("page_1");
    JSONObject page_1 = new JSONObject(page_1Str);
    String city = page_1.getString("city");
    System.out.println(city);
} catch (JSONException e) {
    e.printStackTrace();
}