我是Java新手。我正在我的json字符串中进行一些串联工作。
String JSONstring = {"page_1":"{\"city\":\"Delhi\",\"locality\":\"Alaknanda\",\"Name_of_Person\":\"Varun Patil\",\"User_email_address\":\"varun.vap@gmail.com\",\"user_phone_number\":\"\",\"sub_locality\":\"\",\"street_name\":\"Pune\",\"home_plot_no\":\"xyz\",\"pin_code\":\"411060\",\"project_society_build_name\":\"\",\"landmark_reference_1\":\"\",\"landmark_reference_2\":\"\",\"No_of_Schools\":20,\"No_of_Hospitals\":20,\"No_of_Metro\":0,\"No_of_Mall\":20,\"No_of_Park\":20,\"Distance_of_schools\":1.51,\"Distance_of_Hospitals\":2.5,\"Distance_of_Metro\":0,\"Distance_of_Mall\":1.9,\"Distance_of_Park\":1.1,\"lat\":28.5304408,\"lng\":77.2505733,\"ipinfo\":{\"ip\":\"59.88.97.45\",\"hostname\":\"No Hostname\",\"city\":\"Pune\",\"region\":\"Maharashtra\",\"country\":\"IN\",\"loc\":\"18.5333,73.8667\",\"org\":\"AS9829 National Internet Backbone\",\"postal\":\"411001\"}}","page_2":"{\"home_type\":\"Flat\",\"area\":\"1000\",\"beds\":\"2 BHK\",\"bath_rooms\":2,\"building_age\":\"3\",\"floors\":\"\",\"balcony\":1,\"amenities\":\"regular\",\"amenities_options\":{\"gated_security\":\"\",\"physical_security\":\"\",\"cctv_camera\":\"\",\"controll_access\":\"\",\"elevator\":\"\",\"power_back_up\":\"\",\"parking\":\"\",\"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\":\"\",\"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\":\"\",\"transporation_hub\":\"\",\"shopping_center\":\"\",\"hospital\":\"\",\"school\":\"\",\"ample_parking\":\"\",\"park\":\"\",\"temple\":\"\",\"bank\":\"\",\"less_congestion\":\"\",\"less_pollution\":\"\"},\"maintenance\":\"\",\"maintenance_value\":\"\",\"near_by\":{\"school\":\"\",\"hospital\":\"\",\"mall\":\"\",\"park\":\"\",\"metro\":\"\"},\"city\":\"Delhi\",\"locality\":\"Alaknanda\",\"token\":\"9a2a8bf359494054f98c80009b5bd0e7\"}"}
// removing slashes in JSON input
String jsonFormattedString = JSONString.replaceAll("\\\\", "");
// // removing quote next to the city column
String city_column = replaceCharAt(jsonFormattedString, 10, "");
// // removing quotes at start and end of the inputs
String quote_end = city_column.replaceAll("^\"|\"$", "");
// // removing quote next to page_2
String remove_brace = quote_end.replace("page_2\":\"", "page_2\":");
// // appending braces at the end of the line
String append_brace = remove_brace.replace("}}\"", "}}");
String input = append_brace.substring(0, append_brace.length() - 2);
String final_input = input.concat("}");
JSONObject obj = new JSONObject(final_input);
是否有任何功能可以执行此操作以进行代码优化?
答案 0 :(得分:0)
第一行是无效的Java语句,但假设您只是显示要解析的字符串值,它是一个有效的JSON字符串。
如果您将字符串解析为JSON,您将获得一个包含2个值的JSON对象:page_1
和page_2
。
如果您提取page_1
的值,您将获得以下字符串:
{"city":"Delhi","locality":"Alaknanda","Name_of_Person":"Varun Patil","User_email_address":"varun.vap@gmail.com","user_phone_number":"","sub_locality":"","street_name":"Pune","home_plot_no":"xyz","pin_code":"411060","project_society_build_name":"","landmark_reference_1":"","landmark_reference_2":"","No_of_Schools":20,"No_of_Hospitals":20,"No_of_Metro":0,"No_of_Mall":20,"No_of_Park":20,"Distance_of_schools":1.51,"Distance_of_Hospitals":2.5,"Distance_of_Metro":0,"Distance_of_Mall":1.9,"Distance_of_Park":1.1,"lat":28.5304408,"lng":77.2505733,"ipinfo":{"ip":"59.88.97.45","hostname":"No Hostname","city":"Pune","region":"Maharashtra","country":"IN","loc":"18.5333,73.8667","org":"AS9829 National Internet Backbone","postal":"411001"}}
如果然后将其解析为JSON,您将获得一个JSON对象,其格式如下:
{
"city":"Delhi",
"locality":"Alaknanda",
"Name_of_Person":"Varun Patil",
"User_email_address":"varun.vap@gmail.com",
"user_phone_number":"",
"sub_locality":"",
"street_name":"Pune",
"home_plot_no":"xyz",
"pin_code":"411060",
"project_society_build_name":"",
"landmark_reference_1":"",
"landmark_reference_2":"",
"No_of_Schools":20,
"No_of_Hospitals":20,
"No_of_Metro":0,
"No_of_Mall":20,
"No_of_Park":20,
"Distance_of_schools":1.51,
"Distance_of_Hospitals":2.5,
"Distance_of_Metro":0,
"Distance_of_Mall":1.9,
"Distance_of_Park":1.1,
"lat":28.5304408,
"lng":77.2505733,
"ipinfo":{
"ip":"59.88.97.45",
"hostname":"No Hostname",
"city":"Pune",
"region":"Maharashtra",
"country":"IN",
"loc":"18.5333,73.8667",
"org":"AS9829 National Internet Backbone",
"postal":"411001"
}
}
同样,您也可以解析page_2
。
所以,你所要做的就是对原始输入进行双重解析。