I want this outcome:
{"link":[{"url":"http://en.wikipedia.org/wiki/JScript", "label":"wikipedia"}]}
I tried this:
JSONObject ob1 = new JSONObject();
ob1.put("link","[{\"url\":\"http://en.wikipedia.org/wiki/JavaScript\", label:\"wikipedia\"}]");
The output of ob1.toJSONString() is:
{"link":"[{\"url\":\"http:\/\/en.wikipedia.org\/wiki\/JavaScript\", label:\"wikipedia\"}]"}
What am I doing wrongly?
I am using json-simple-1.1.1
You should put a JSONArray into the JSONObject and in the Array again a JSONObject with the keys/values "url"/"http://en.wikipedia.org/wiki/JScript" and "label"/"wikipedia"
JSONObject ob1 = new JSONObject();
JSONArray ar1 = new JSONArray();
ob1.put ("link", ar1);
JSONObject ob2 = new JSONObject();
ar1.add(ob2);
ob2.put("url", "http://en.wikipedia.org/wiki/JScript");
ob2.put("label", "wikipedia");
In case you have the value of ob1 link object already as JSON string then you can first interpret this into a JSONArray using
JSONArray ar1 = (JSONArray)JSONValue.parse(yourJSONStr);
NOTE: Your intended result is not a JSON string, because for that all "/" must be escaped "\/"
Related
I'm trying to read a JSONObject from within another JSONObject. The first output works, the second, i.e. the JSONObject within, does not. Here I get the following error
org.json.JSONException: JSONObject["Values"] not found.
This is the output of the first object / array
ergebnis: {"Description":"","Objects":[{"RefStr":"76-1044-0","ClassName":"Application","Values":{"providerorg-refstr":"262-462-0",...}]}
This is how I try to get the data:
JSONObject jsonObject = new JSONObject(ergebnis);
System.out.println(jsonObject);
JSONObject inside = jsonObject.getJSONObject("Values");
System.out.println(inside);
Values object is present in Objects. This objects is an array. You can try like below,
JSONObject jsonObject = new JSONObject(json);
System.out.println(jsonObject);
JSONObject jsonObject1= jsonObject.getJSONObject("ergebnis");
System.out.println(jsonObject1);
JSONArray objects = jsonObject1.getJSONArray("Objects");
System.out.println(objects);
JSONObject inside = objects.getJSONObject(0).getJSONObject("Values");
System.out.println(inside);
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);
I want to convert my Blank Json array to the Null Json array.
For ex., My Json array is like "[{}]" and if I got this array then automatically converts to "[]".
My code is like as below :
JsonObject jo = FetchData.getAllItemsAvg(request.getParameter("where"), request.getParameter("lastNum"),request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
ja.add(jo); // Some times ja like "[{}]" .
Check if the object is empty before adding it to the array.
(assuming you're using JsonObject):
JsonObject jo = FetchData.getAllItemsAvg(
request.getParameter("where"),
request.getParameter("lastNum"),
request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
if(!jo.isEmpty()){
ja.add(jo);
}
For com.google.gson.JsonObject:
JsonObject jo = FetchData.getAllItemsAvg(
request.getParameter("where"),
request.getParameter("lastNum"),
request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
if(!jo.entrySet().isEmpty()){
ja.add(jo);
}
Servlet side. I write 2 JSON String objects to JSON array:
Gson data = new Gson();
JsonArray arr = new JsonArray();
JsonObject obj1 = new JsonObject();
JsonElement element = data.toJsonTree(imgstr);
obj1.add("image", element);
arr.add(obj1);
JsonObject obj2 = new JsonObject();
JsonElement element1 = data.toJsonTree(strBuf);
obj2.add("html", element1);
arr.add(obj2);
out.write(arr.toString());
Ajax. I receive that array:
done(function(result) {
console.log(result);
})
It shows [{"image":"myImageString"},{"html":"myHtmlString"}].
How to get that Strings separately? Following doesn't work:
var image=result.image;
var html=result.html;
Your result in this case in an array of objects. So you should acces them as an array:
var image=result[0].image;
var html=result[1].html;
It would be better though to just return one object:
JsonObject obj1 = new JsonObject();
JsonElement element = data.toJsonTree(imgstr);
obj1.add("image", element);
obj1.add("html", element1
out.write(obj1.toString());
In which case your suggested code
var image=result.image;
var html=result.html;
will work.
if you sending an array, you must retrieve objects from array by index like
var image=result[0].image;
But if you want to get objects by name like
var image=result.image;
you must send JsonObject not a JsonArray
I need to create in a Java (Android) a JSONObject/JSONArray with the exact following structure:
{
"listId":
[
"c02bc683-fcd7-47a5-b157-853e26ed099e",
"f8e1c9d7-ae45-4433-a315-726c1d912d09"
]
}
Can somebody help me on this please?
EDIT:
What i have is this:
JSONArray obj = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("listId", folderId);
obj.put(jsonObject);
JSONObject json = new JSONObject();
json.put("id", obj);
But this produces something like:
{
"listId":
[
{"id":"c02bc683-fcd7-47a5-b157-853e26ed099e"},
{"id":"f8e1c9d7-ae45-4433-a315-726c1d912d09"}
]
}
Thanks
You've got your array creation a bit mixed up. What you really want is an object holding an array of string, but what you're doing is creating an array of JSONObject.
Try this instead:
String[] arr = { "c02bc683-fcd7-47a5-b157-853e26ed099e", "f8e1c9d7-ae45-4433-a315-726c1d912d09" };
JSONArray jsonArray = new JSONArray(arr);
JSONObject json = new JSONObject();
json.put("listId", jsonArray);
System.out.println(json); // {"listId":["c02bc683-fcd7-47a5-b157-853e26ed099e","f8e1c9d7-ae45-4433-a315-726c1d912d09"]}