0

I am new to Java. I am doing some concatenation work in my json string like this.

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

is There any function to do this for code optimization ?

moha
  • 579
  • 2
  • 9
  • 27
  • What is it you're trying to do? – Andreas Feb 16 '16 at 07:08
  • OMG! It is very big string to work on. As per my knowledge there is no such function for this. Apart from above functions you can use String.split(regex) so as to get the pair after removing unnecessary characters or words. – Suyash Feb 16 '16 at 07:08
  • Your question is unclear. The first line is not valid Java, and the rest of the code is extremely brittle. It will break as soon as ANYTHING in the layout of the JSON string changes. Why don't you want to use a [real JSON parser](https://github.com/FasterXML/jackson)? – Jim Garrison Feb 16 '16 at 07:10
  • I dont know When I pass my json string it throws some error because of some format issue.Any help to check the json string or any other decode options ?? – Evangeline Lydia Feb 16 '16 at 07:10
  • Well, for one, that first line won't compile in Java, so you can't actually get a runtime error. – Andreas Feb 16 '16 at 07:11
  • yes i am getting compile time error.but I will pass thw json string in run time only. – Evangeline Lydia Feb 16 '16 at 07:12
  • try JSON parsor and formatter : https://jsonformatter.curiousconcept.com/ – Suyash Feb 16 '16 at 07:12
  • Maybe this SO answer is helpful for the start: http://stackoverflow.com/questions/1395551/convert-a-json-string-to-object-in-java – SubOptimal Feb 16 '16 at 07:18
  • what is the compile error that you get? please add it to the question. I suspect it is the first line - I and others can see that this line is wrong (not valid Java code). – EkcenierK Feb 16 '16 at 09:28

1 Answers1

0

The first line is an invalid Java statement, but assuming that you simply showed the string value to be parsed, it is a valid JSON string.

If you parse the string as JSON, you will get a JSON object with 2 values: page_1 and page_2.

If you extract the value of page_1, you will get the following string:

{"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"}}

If you then parse that as JSON, you will get a JSON object, which formatted looks like this:

{
   "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"
   }
}

Similarly, you can also parse the value of page_2.

So, all you have to do, is double-parse the original input.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    @EvangelineLydia Nope. At the detail level of "nothing works", my answer would be "try changing something", but that's not really helpful, now is it? Maybe you should read "[How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)" – Andreas Feb 16 '16 at 07:52