JSON pointer builder breaks when array is put() in jsonObject - java
I'm learning how to use JSON in java and tried building and reading JSON and found this.
int array[] = {1,2,3,4,5};
JSONObject obj = new JSONObject();
obj.put("putArray", array);
obj.append("appendArray", array);
JSONPointer pointer = JSONPointer.builder()
.append("putArray")
.build();
System.out.print(pointer.queryFrom(obj));
When I append "appendArray" to the JSON pointer it outputs the expected [[1,2,3,4,5]]
But if I append the put array I get [I#6ce253f1
I tried using JSONArray but the problem occured again if you append the array index 0 to the pointer
JSONArray jArray = new JSONArray();
jArray.put(array);
obj.put("array1", jArray);
obj.append("array2", jArray);
JSONPointer pointer = JSONPointer.builder()
.append("array1")
.append(0)
.build();
System.out.print(pointer.queryFrom(obj));
I know there are better ways to parse through JSON but I don't know what is happening. Why am I getting this weird result? What does put() vs append() change where it causes this result?
Related
Storing JsonObjects in an ArrayList in Java
I am trying to store a multiple JsonObjects in ArrayList to parse and display in tableview at later stage. for some odd reason I can't add objects to the list. I am using javax.json Here is my Try statement: try { JsonReader jsonReader = Json.creatReader(new StringReader(test)); JsonObject obj = jsonReader.readObject(); jsonReader.close(); key = obj.getString("key"); // this works with no issue ArrayList<JsonObject> jsonList = new ArrayList<>(); jsonList.add(obj); // everything hangs with no errors when I try to do this } debugger shows jsonList size 0 obj size 3 for this line jsonList.add(obj); if I commented these lines everything works as expected ArrayList<JsonObject> jsonList = new ArrayList<>(); jsonList.add(obj); when they are not commented I am getting catch final throwable (be sure to set the state after the cause of failure.
Extract Fields from a Json String in Java
I am trying to extract the ids from each company_id from the following Json String String test = ["{\"company_id\":4100,\"data\":{\"drm_user_id\":572901936637129135,\"direct_status_id\":0,\"direct_optin_date\":0,\"direct_first_optin_date\":0,\"direct_last_optin_date\":0,\"direct_optout_date\":0,\"direct_last_form_date\":0,\"direct_last_form_id\":0,\"direct_last_promo_id\":0,\"anon_status_id\":600,\"anon_optin_date\":1446132360498,\"anon_first_optin_date\":1446132360498,\"anon_last_optin_date\":1446132360498,\"anon_optout_date\":0,\"anon_last_form_date\":1446132360498,\"anon_last_form_id\":101,\"anon_last_promo_id\":1002003,\"last_registration_date\":1446132360498,\"mp_status_id\":600,\"mp_control_state\":-1,\"mp_match_date\":0,\"mp_vs_version\":0,\"mp_initial_value_segment\":0,\"mp_id\":0,\"conversion_last_form_date\":0,\"conversion_last_form_id\":0,\"conversion_last_promo_id\":-1,\"last_message_date\":1446132368928,\"cg_version\":0,\"cg_version_date\":0,\"num_anon_messages_global\":0,\"num_anon_messages_global_date\":0,\"reg_creator_id\":576,\"reg_form_id\":101,\"reg_method_id\":1,\"reg_creator_type_id\":1},\"personal_data\":{\"version\":0,\"personal_data\":\"{}\",\"mdc_data\":{\"version\":0},\"custom_data\":\"{}\"},\"category_data\":{},\"campaignImpressions\":{},\"journeyStartDate\":0}","{\"company_id\":4045,\"data\":{\"drm_user_id\":572901936637129135,\"direct_status_id\":0,\"direct_optin_date\":0,\"direct_first_optin_date\":0,\"direct_last_optin_date\":0,\"direct_optout_date\":0,\"direct_last_form_date\":0,\"direct_last_form_id\":0,\"direct_last_promo_id\":0,\"anon_status_id\":600,\"anon_optin_date\":1446132360498,\"anon_first_optin_date\":1446132360498,\"anon_last_optin_date\":1446132360498,\"anon_optout_date\":0,\"anon_last_form_date\":1446132360498,\"anon_last_form_id\":101,\"anon_last_promo_id\":1002003,\"last_registration_date\":1446132360498,\"mp_status_id\":600,\"mp_control_state\":-1,\"mp_match_date\":0,\"mp_vs_version\":0,\"mp_initial_value_segment\":0,\"mp_id\":0,\"conversion_last_form_date\":0,\"conversion_last_form_id\":0,\"conversion_last_promo_id\":-1,\"last_message_date\":1446132368928,\"cg_version\":0,\"cg_version_date\":0,\"num_anon_messages_global\":0,\"num_anon_messages_global_date\":0,\"reg_creator_id\":576,\"reg_form_id\":101,\"reg_method_id\":1,\"reg_creator_type_id\":1},\"personal_data\":{\"version\":0,\"personal_data\":\"{}\",\"mdc_data\":{\"version\":0},\"custom_data\":\"{}\"},\"category_data\":{},\"campaignImpressions\":{},\"journeyStartDate\":0}","{\"company_id\":2979,\"data\":{\"drm_user_id\":572901936637129135,\"direct_status_id\":0,\"direct_optin_date\":0,\"direct_first_optin_date\":0,\"direct_last_optin_date\":0,\"direct_optout_date\":0,\"direct_last_form_date\":0,\"direct_last_form_id\":0,\"direct_last_promo_id\":0,\"anon_status_id\":600,\"anon_optin_date\":1446132360498,\"anon_first_optin_date\":1446132360498,\"anon_last_optin_date\":1446132360498,\"anon_optout_date\":0,\"anon_last_form_date\":1446132360498,\"anon_last_form_id\":101,\"anon_last_promo_id\":1002003,\"last_registration_date\":1446132360498,\"mp_status_id\":600,\"mp_control_state\":-1,\"mp_match_date\":0,\"mp_vs_version\":0,\"mp_initial_value_segment\":0,\"mp_id\":0,\"conversion_last_form_date\":0,\"conversion_last_form_id\":0,\"conversion_last_promo_id\":-1,\"last_message_date\":1446132368928,\"cg_version\":0,\"cg_version_date\":0,\"num_anon_messages_global\":0,\"num_anon_messages_global_date\":0,\"reg_creator_id\":576,\"reg_form_id\":101,\"reg_method_id\":1,\"reg_creator_type_id\":1},\"personal_data\":{\"version\":0,\"personal_data\":\"{}\",\"mdc_data\":{\"version\":0},\"custom_data\":\"{}\"},\"category_data\":{},\"campaignImpressions\":{},\"journeyStartDate\":0}"] I am very new to working with Json. This is what I have so far: JSONObject jsonObject = new JSONObject(test); JSONObject newJSON = jsonObject.getJSONObject("company_id"); System.out.println(newJSON.toString()); jsonObject = new JSONObject(newJSON.toString()); I am stuck here because I honestly do not know how would I extract the company_id. Thank you for your time and patience.
String test = ["{\"company_id\":4100,\"data\":{\"drm_user_id\":572901936637129135,\"direct_status_id\":0,\"direct_optin_date\":0,\"direct_first_optin_date\":0,\"direct_last_optin_date\":0,\"direct_optout_date\":0,\"direct_last_form_date\":0,\"direct_last_form_id\":0,\"direct_last_promo_id\":0,\"anon_status_id\":600,\"anon_optin_date\":1446132360498,\"anon_first_optin_date\":1446132360498,\"anon_last_optin_date\":1446132360498,\"anon_optout_date\":0,\"anon_last_form_date\":1446132360498,\"anon_last_form_id\":101,\"anon_last_promo_id\":1002003,\"last_registration_date\":1446132360498,\"mp_status_id\":600,\"mp_control_state\":-1,\"mp_match_date\":0,\"mp_vs_version\":0,\"mp_initial_value_segment\":0,\"mp_id\":0,\"conversion_last_form_date\":0,\"conversion_last_form_id\":0,\"conversion_last_promo_id\":-1,\"last_message_date\":1446132368928,\"cg_version\":0,\"cg_version_date\":0,\"num_anon_messages_global\":0,\"num_anon_messages_global_date\":0,\"reg_creator_id\":576,\"reg_form_id\":101,\"reg_method_id\":1,\"reg_creator_type_id\":1},\"personal_data\":{\"version\":0,\"personal_data\":\"{}\",\"mdc_data\":{\"version\":0},\"custom_data\":\"{}\"},\"category_data\":{},\"campaignImpressions\":{},\"journeyStartDate\":0}","{\"company_id\":4045,\"data\":{\"drm_user_id\":572901936637129135,\"direct_status_id\":0,\"direct_optin_date\":0,\"direct_first_optin_date\":0,\"direct_last_optin_date\":0,\"direct_optout_date\":0,\"direct_last_form_date\":0,\"direct_last_form_id\":0,\"direct_last_promo_id\":0,\"anon_status_id\":600,\"anon_optin_date\":1446132360498,\"anon_first_optin_date\":1446132360498,\"anon_last_optin_date\":1446132360498,\"anon_optout_date\":0,\"anon_last_form_date\":1446132360498,\"anon_last_form_id\":101,\"anon_last_promo_id\":1002003,\"last_registration_date\":1446132360498,\"mp_status_id\":600,\"mp_control_state\":-1,\"mp_match_date\":0,\"mp_vs_version\":0,\"mp_initial_value_segment\":0,\"mp_id\":0,\"conversion_last_form_date\":0,\"conversion_last_form_id\":0,\"conversion_last_promo_id\":-1,\"last_message_date\":1446132368928,\"cg_version\":0,\"cg_version_date\":0,\"num_anon_messages_global\":0,\"num_anon_messages_global_date\":0,\"reg_creator_id\":576,\"reg_form_id\":101,\"reg_method_id\":1,\"reg_creator_type_id\":1},\"personal_data\":{\"version\":0,\"personal_data\":\"{}\",\"mdc_data\":{\"version\":0},\"custom_data\":\"{}\"},\"category_data\":{},\"campaignImpressions\":{},\"journeyStartDate\":0}","{\"company_id\":2979,\"data\":{\"drm_user_id\":572901936637129135,\"direct_status_id\":0,\"direct_optin_date\":0,\"direct_first_optin_date\":0,\"direct_last_optin_date\":0,\"direct_optout_date\":0,\"direct_last_form_date\":0,\"direct_last_form_id\":0,\"direct_last_promo_id\":0,\"anon_status_id\":600,\"anon_optin_date\":1446132360498,\"anon_first_optin_date\":1446132360498,\"anon_last_optin_date\":1446132360498,\"anon_optout_date\":0,\"anon_last_form_date\":1446132360498,\"anon_last_form_id\":101,\"anon_last_promo_id\":1002003,\"last_registration_date\":1446132360498,\"mp_status_id\":600,\"mp_control_state\":-1,\"mp_match_date\":0,\"mp_vs_version\":0,\"mp_initial_value_segment\":0,\"mp_id\":0,\"conversion_last_form_date\":0,\"conversion_last_form_id\":0,\"conversion_last_promo_id\":-1,\"last_message_date\":1446132368928,\"cg_version\":0,\"cg_version_date\":0,\"num_anon_messages_global\":0,\"num_anon_messages_global_date\":0,\"reg_creator_id\":576,\"reg_form_id\":101,\"reg_method_id\":1,\"reg_creator_type_id\":1},\"personal_data\":{\"version\":0,\"personal_data\":\"{}\",\"mdc_data\":{\"version\":0},\"custom_data\":\"{}\"},\"category_data\":{},\"campaignImpressions\":{},\"journeyStartDate\":0}"] JSONParser parser = new JSONParser(); Object obj = parser.parse(test); JSONArray array = (JSONArray)obj; System.out.println(array.get("company_id"));
I think there is a object array. I think you can get you answer using following way. for(int i=0;i<array.length();i++){ String company_id=array.getJSONObject(i).getString("company_id"); }
Java - looping through JSONArray
i am trying to call an URL and afterwards to save the results of the URL into a database. The call of the URL is working and i also am able to save the result into JSON objects/arrays. This is my code so far: JSONParser parser = new JSONParser(); try { // responseString is the answer i get from calling the URL. // It's pretty long that's why i don't copy it in here now // But you can call this URL to see what the result is: // http://www.gw2spidy.com/api/v0.9/json/items/all/1?filter_ids=29169,29185 Object objToParse = parser.parse(responseString); JSONObject jsonObject = (JSONObject) objToParse; JSONArray array = (JSONArray) jsonObject.get("results"); // Until here everything is ok, the results get saved into the array JSONObject mJsonObject = new JSONObject(); for (int i = 0; i < array.length() ; i++) { mJsonObject = (JSONObject)array.get(i); System.out.println(mJsonObject); } } catch(ParseException pe) { System.out.println("position: " + pe.getPosition()); System.out.println(pe); } When i try to run this i get an error when i try to loop through the array: Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to java.lang.CharSequence I already searched for solutions but i cannot find or understand what is causing the error for me, would be nice if someone can help me here..
Ok at the end this was working for me: JSONObject jsonObject = new JSONObject(responseString); JSONArray array = (JSONArray) jsonObject.get("results"); JSONObject mJsonObject = new JSONObject(); for (int i = 0; i < array.length() ; i++) { mJsonObject = (JSONObject)array.get(i); System.out.println(mJsonObject); } Had to change org.json.simple to instead use org.json and changed some lines then it was working.
I rather think that your implementation using simple json was wrong. Although you didn't mention exactly, the only place your Exception could happen would be the line JSONArray array = (JSONArray) jsonObject.get("results"); and as this is the same in both implementations, something previous to that must have happened leading to a situation with simple json where the results property is not a JSONArray. Probably something with parser.parse(...).
how to read JSON from HTTP GET request?
I have created a java server which gets HTTP GET request url as /Star/getDetails?sentMsg=data.requestorName:ABC,data.companyName:EFG,portfolios: [{name:,placeholder:Portfolio 1,positions:[{ticker:T1234,weight:29.85}, {ticker:T2345,weight:70.15}],active:false}],analyticsDate:20140630} I have to parse sentMsg parameter such as I am able to read each variable individually. For eg, i should be able to read data.requestorName, companyName. I am not able to find a way to do it. request.getParameter("sentMsg") always return String. Tried parsing it through json-simple JSONParser jp = new JSONParser(); try { Object obj = jp.parse(sentMsg); JSONArray ja = (JSONArray)obj; } catch (ParseException e) { e.printStackTrace(); } But this gives parse exception. I have limitation to use json-simple jar only. Any suggestion on how to do it?
Get the paramter sentMsg from HttpRequest object store it into a string. Split from comma i.e. "," and the last second token would be the json string. You can now parse it using Json simple lib and extract values from it.
Provided you have valid JSON like: private static String jsonString = "[{name : \"stackOverFlow\"}]"; Convert it to JSONArray like: JSONArray jsonArray = new JSONArray(jsonString ); Then you can get value out of JSONArray by looping through it: for (int i = 0; i < jsonArray.length(); i++) { //Iterating over mediaArray JSONObject media = jsonArray.getJSONObject(i); String nameFromJSON = media.getString("name"); System.out.println("Name = " + nameFromJSON); } Output will be: //Name = stackOverFlow
how do i add String array to JSON
I am currently writing some code in a servlet that gets data from the database and returns it to the client. What I am having problems with is inserting the array of dates I have collected and adding them to my JSON object that I will return to the client. Here is the code I'm trying but it keeps giving errors dates = ClassdatesDAO.getdate(dates); ArrayList<String> ClassDates = new ArrayList<String>(); ClassDates = dates.getClassdates(); response.setContentType("application/json"); JSONObject Dates = new JSONObject(); Dates.put("dates", new JSONArray(ClassDates)); In my IDE I get this error over the ClassDates in the JSONArray The constructor JSONArray(ArrayList) is undefined
You are passing ArrayList instance instead of an Array. So, convert the list into an array and then pass it as an argument like this Dates.put("dates", new JSONArray(ClassDates.toArray(new String[ClassDates.size()]))); Note : json API has a method signature accepting java.util.Collection. So, you are using some other library or older version
JSONObject Dates = new JSONObject(); JSONArray datesJSONArray = new JSONArray(); for (String date : ClassDates) datesJSONArray.put(date); try { Dates.put("dates", datesJSONArray); } catch (JSONException e) { e.printStackTrace(); }