Json full of "\" - java

I'm putting a json inside another like this:
JSONObject json = new JSONObject();
for(.....){
JSONObject v = new JSONObject();
v.put("key", "value");
v.put("key", "value");
v.put("key", "value");
json.put(i, v.toString());
i++;
}
The problem is that it is getting like this:
{"0":"{\"key\":\"value\",\"key\":\"value\",...}"}
Filled with "\", does anyone know how to withdraw or do the "right mode"?

cause you put string in json. change this:
from:
json.put(i, v.toString());
to:
json.put(i, v);

Related

How to set array in jsonobject

Unable to set array inside JSONObject according to the response.Below is my code in which I am unable to set array in jsonobject. How to send key value for array inside my jsonobject for which shared the response which code is getting from postman
Is this the right way in code
Code--
JsonArray array = new JsonArray();
array.add(productId);
array.add(qty);
JSONObject jsonObject = new JSONObject();
jsonObject.put("productDetails", array);**
This is the code in MainActivity. The problem is not getting correct jsonarray in my JSON object so API will not hit correctly
These String key values are used to pass in request params
String key="WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz";
String affId="teamfotog";
String act="photoStores";
String latitude="40.7127753";
String longitude="-74.0059728";
String devinf="Android,7.0";
String appver="1.00";
String productId="6670002";
String qty="3";
//productDetails
**JsonArray array = new JsonArray();
array.add(productId);
array.add(qty);**
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("apiKey", key);
jsonObject.put("affId", affId);
jsonObject.put("act", act);
jsonObject.put("latitude", latitude);
jsonObject.put("longitude", longitude);
jsonObject.put("devinf", devinf);
jsonObject.put("appver", appver);
**jsonObject.put("productDetails", array);**
JsonParser jsonParser = new JsonParser();
ApiStorePhotoInterface apiInterface = ApiStorePhotoClient.getApi();
Call<PhotoStoreMainModel> call = apiInterface.getResponse((JsonObject) jsonParser.parse(jsonObject.toString().trim()));
Request Params is in Jsonbody --
{"apiKey":"WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz","affId":"teamfotog","act":"photoStores","latitude":"40.7127753","longitude":"-74.0059728","devinf":"Android,7.0","appver":"1.00","productDetails":[{"productId":"6670002","qty":"3"}]}
Of course, it won't work. You are directly adding objects(Strings) in your JsonArray. In the response body, what you really want is a JsonObject inside the JsonArray. Try this -
JsonObject productDetail = new JsonObject();
productDetail.addProperty("productId", productId);
productDetail.addProperty("qty", qty);
JsonArray array = new JsonArray();
array.add(productDetail);
Try this .
jsonObject.put("productDetails",(Object)array);
If someone is still having problems with this, this is what fixed it for me:
Use:
JSONArray
istead of
JsonArray
You should put product detail model to array
String key="WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz";
String affId="teamfotog";
String act="photoStores";
String latitude="40.7127753";
String longitude="-74.0059728";
String devinf="Android,7.0";
String appver="1.00";
String productId="6670002";
String qty="3";
JsonObject product = new JsonObject();
product.put("productId",productId);
product.put("qty",qty);
JsonArray array = new JsonArray();
array.add(product);
JSONObject jsonObject = new JSONObject();
jsonObject.put("apiKey", key);
jsonObject.put("affId", affId);
jsonObject.put("act", act);
jsonObject.put("latitude", latitude);
jsonObject.put("longitude", longitude);
jsonObject.put("devinf", devinf);
jsonObject.put("appver", appver);
jsonObject.put("productDetails", array);

Java Rest Template

ResponseEntity<BaseDto> entity = restTemplate.getForEntity("/get/code/IN", BaseDto.class);
System.out.println("entity : " + entity);
System.out.println("entity.getBody() : " + entity.getBody());
System.out.println(entity.getBody().getResponseObject());
As per above rest am getting below format:
{
systemTrack=
{
createUser=admin,
createDate=2016-03-01 18:11:17,
lastUpdatedUser=admin,
lastUpdatedDate=2016-03-01 18:11:17
},
countryCode=IN, countryName=INDIA
}
How to get the values from this format?
Maybe I was unclear with my question, but i got a solution through debug mode.
It was a format which is in Linked hash map ,so what I did was I just wrote below code for json conversion of LinkedHashMap.
LinkedHashMap m = (LinkedHashMap) object;
JSONObject jsonObject = new JSONObject(m);(package of org.json.JSONObject)
and then i assigned to custom class object using
Gson gson = new Gson();
String jsonString = jsonObject.toString();
Object obj = gson.fromJson(jsonString, type);

JsonObject in Java

Original Question: Resolved
I'm producing Json from the controller using JsonObject/JsonArray:
JSONObject headers = new JSONObject();
headers.put("ID", "");
headers.put("Organization Name", "");
headers.put("Submission Date", "");
headers.put("Status", "");
JSONObject organizationsHJ = new JSONObject();
organizationsHJ.put("headers", headers);
array.add(organizationsHJ );
This produces JSON as:
"headers":{"Status":"","Submission Date":"","Organization Name":"","ID":""}
Instead I need to get the output as:
"headers":[ "ID", "Organization Name", "Submission Date", "Status" ]
Is that possible? Please advise. Please note I can have the JSON as a javascript variable as well if that's easier?
EDIT:
I need one more simple change in the JSON output. Here is my code:
JSONObject notifications = new JSONObject();
notifications.put("id", "1");
notifications.put("description", "Notification 1");
notifications.put("createdTimestamp", "2015-05-12T18:15:28.237Z");
notifications.put("startTimestamp", "2015-05-25T18:30:28.237Z");
notifications.put("endTimestamp", "2015-06-13T12:30:30.237Z");
notifications.put("active", "true");
This generates JSON output as:
{"data":{"id":"1","createdTimestamp":"2015-05-12T18:15:28.237Z","description":"Notification 1","startTimestamp":"2015-05-25T18:30:28.237Z","active":"true","createdId":"251","endTimestamp":"2015-06-13T12:30:30.237Z"}}
Instead I want it to generate as:
{"data":["id":"1","createdTimestamp":"2015-05-12T18:15:28.237Z","description":"Notification 1","startTimestamp":"2015-05-25T18:30:28.237Z","active":"true","createdId":"251","endTimestamp":"2015-06-13T12:30:30.237Z"]}
headers should be a JSONArray, not JSONObject. An object is a set of name-to-value mappings, an array is a linear collection.
JSONArray headers = new JSONObject();
headers.add("ID");
headers.add("Organization Name");
headers.add("Submission Date");
headers.add("Status");
JSONObject organizationsHJ = new JSONObject();
organizationsHJ.put("headers", headers);
array.add(organizationsHJ );
organizationsHJ.put("headers", headers.keySet());

Error when using JSONSimple to write a json file with for loop

I need to transform my data in a ArrayList to a JSON file, and I use JSON.simple. Everything is fine except one little thing that I want to get a result like ...... {source:0, target:1},{source:0, target:1},{source:0, target:2},{source:0, target:3} ...... but it returns ......{source:0, target:16},{source:0, target:16},{source:0, target:16}...... . My solution.size() is 17.
Here is my code:
JSONObject jsonObject = new JSONObject();
JSONObject jsonNodesObject = new JSONObject();
JSONObject jsonEdgesObject = new JSONObject();
JSONArray jsonNodesArray = new JSONArray();
JSONArray jsonEdgesArray = new JSONArray();
String instString = solutions.get(0).get("institution");
jsonNodesObject.put("name", instString);
// extract name and institution from ArrayList
for (int i = 0; i < solutions.size(); i++)
{
HashMap<String, String> eleHashMap= solutions.get(i);
String nameString = eleHashMap.get("name");
jsonNodesObject.put("name", nameString);
jsonNodesArray.add(jsonNodesObject);
jsonEdgesObject.put("source", 0);
jsonEdgesObject.put("target", i);
jsonEdgesArray.add(jsonEdgesObject);
}
jsonObject.put("nodes", jsonNodesArray);
jsonObject.put("edges", jsonEdgesArray);
System.out.println(jsonObject);
It seems that in every for loop, it refreshes the value of target: i of all my jsonEdgesArray.
Dose anyone know how to fix this? Thanks in advance!
As your iterating jsonNodesObject in for loop, same value will be put for jsonNodesObject.put("name", nameString); u have to initialize JSONObject jsonNodesObject = new JSONObject(); inside for loop

Need to change my code to generate correct JSON object

I am trying to generate the following JSON to be accessed by tokenInput, but my code generates it in other way, how can I tailor the code to generate the correct JSON.
Required Json
get[{id:"set",name:"set"},{id:"object",name="object}]
or
[{id:"set",name:"set"},{id:"object",name="object}]
My JSON
{"get":[{"id":"set","name":"set"},{"id":"object","name":"object"}]}
MyCode
JSONObject res = new JSONObject();
JSONArray jarray = new JSONArray();
JSONObject object = new JSONObject();
object.put("id","set");
object.put("name", "set");
jarray.add(object);
JSONObject object1 = new JSONObject();
object1.put("id","object");
object1.put("name", "object");
jarray.add(object1);
res.put("get", jarray);
return res;
JSP
$(document).ready(function() {
$("#items").tokenInput("getOptions.jsp",{
theme: "facebook"
});
});
[{"id":"set","name":"set"},{"id":"object","name":"object"}]
this can be only form can get instead of form ur code generates . make res a JSONArray . then try .. !

Categories