Extract cloudant document in java - java

I received response from cloudant and I want parse the response.
My response is,
{"_id":"id_name_list","_rev":"5-9e6b089e3094955395d1c976c5d87392","1":"aa","2":"bb","3":"cc","4":"dd"}
Code to convert response to JSON is,
JSONObject jo = new JSONObject(response);
String s = jo.get("1");
I am getting the error:
org.json.JSONException: JSONObject["1"] not found.

Related

SQS - Sending payload with JSONObject push empty objects

I was testing different payload structures accepted by AWS SDK (org.springframework.cloud:spring-cloud-starter-aws-messaging:2.2.6.RELEASE). I am sending the message using convertAndSend function provided by QueueMessagingTemplate. I am able to send it successfully with a string payload or a custom java object. However, when I convert my custom java object to a JSONObject, and push the JSONObject to SQS, it seems the the messageBody being pushed is {empty:true}. When I send it with jsonObject.toString(), it works well though. I am confused on why convertAndSend works for custom java class/object but not for JSONObject type.
Below is a sample code on how I am doing the JSON conversion:
public JSONObject toJson() throws Exception {
JSONObject json = new JSONObject();
json.put("payload", this.payload);
json.put("id", this.taskId);
return json;
}
SQS only allows message content that is JSON formatted string. JSONObject is a Java object. To get it to a JSON formatted string your method needs to look like this:
public String toJson() throws Exception {
JSONObject json = new JSONObject();
json.put("payload", this.payload);
json.put("id", this.taskId);
return json.toString();
}

how to convert json into string without line break and the other format

I'm setting up a new server and android, and want to send base64(image) from android to server. I use JSON for data format between android and server, so I have to put my base64 into that JSON, but the server cannot decode my base64 because there are a symbol like "\n,+," and many more. I also try using Notepad for replace "\n" but still the server wont decode it..
note:
if I use Postman the server work properly
JSONArray data = new JSONArray();
for (int i = 0; i < listImage.size(); i++) {
//listImage.get(i).setBase64(map.get(listImage.get(i).getId()));
jsonObject = new JSONObject();
jsonObject.put("id",listImage.get(i).getId());
jsonObject.put("base64",map.get(listImage.get(i).getId()));
data.put(jsonObject);
}
jsonObject = new JSONObject();
jsonObject.put("image",data);
JSONObject has toString() method that return json object data into String format. You can also look at UTF encoding to encode special character, to make http request without conflicting of special character in URL.
jsonObject = new JSONObject();
jsonObject.put("image",data);
jsonObject.toString();
Easiest way to convert jason object to a String
JSONObject json = new JSONObject();
json.toString();
thank you for your answer guys...I have found my own solution, I need to replace "\n" and "\" with "" from base64 in server side

How to get ArrayList<String[]> from a JSONObject

Actually, I have a JSONObject like this:
JSONObject json = new JSONObject();
json.put("type", "login");
json.put("friendList", FriendList);
and the FriendListis the type of ArrayList<String[]>
, then I use a socket to transfer JSON to my client.
My client received the data:
JSONObject receive_msg = new JSONObject(data);
String type = receive_msg.getString("type");
My question is how to get friendList with data typeArrayList<String[]>?
Thanks a lot if anyone helps.
you need to use JsonArray, iteratore over your list and fill the jsonArray then put it in the JsonObject
http://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html

gson.fromJson() method converting invalid json to valid json

In my program i am taking input from rest client in json format and retrieving the json from request body i am using gson.fromJson method for retrieving json.
BufferedReader reader = request.getReader();
JsonElement jsonElement = gson.fromJson( reader , JsonElement.class );
Input json, i am passing to rest client :
It is not a valid json.
{
"buildName":FHGF"",
"deadline":"2017-07-28T23:11:15+00:00",
"release":"0.2",
"platformRelease":"5.10.0",
"platformBuild":"1.0.0",
"releatedProducts":"##Product*****",
"expiresInPronto":"2017-07-28T23:11:15+00:00",
"changeNotesRequired":"yes",
"environment":"ah",
"stateTestedInUse":"no"
}
Json which is created by gson :
{
**"buildName":"FHGF\"\""**,
"deadline":"2017-07-28T23:11:15+00:00",
"release":"0.2",
"platformRelease":"5.10.0",
"platformBuild":"1.0.0",
"releatedProducts":"##Product*****",
"expiresInPronto":"2017-07-28T23:11:15+00:00",
"changeNotesRequired":"yes",
"environment":"ah",
"stateTestedInUse":"no"
}
Instead of throwing error gson has converted it to valid json.

How to add json to GET request query parameter?

I want json to be sent to GET request in query parameter to get the response for that json request.
If I use a link something like this :
www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}
Then the url part appears blue if I do it as sysout statement, something like this:
www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}
and the json appears as if it is not the part of the request.
To create a URL, I'm appending the manipulated JSON string to the URL and then sending the request. But it appears as two different strings.
Also I have used encoder to encode the JSON part
filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}
In that case the brackets and double quotes everything in that json is encoded, even the equalTo sign. Also the link appears blue but while sending request it throws exception of 400 Bad Request, since the equalTo is also converted to its encoding format.
I tried encoding only the JSON part leaving the filter= in the url itself, something like this :
www.url.com/search?query1=abc&filter={"or":[{"terms":{"vin":["1g1105sa2gu104086"]}}]}
The results that appear after the request is send is different from the results I want.
I'm using following code to create a JSON:
private String getVinFromInventoryRequest(String vin) throws JSONException {
JSONObject request = new JSONObject();
JSONArray orArray = new JSONArray();
for(String vin : vins) {
JSONObject termsObject = new JSONObject();
JSONObject vinsObject = new JSONObject();
JSONArray vinsArray = new JSONArray();
vinsArray.put(vin);
vinsObject.put("vin", vinsArray);
termsObject.put("terms", vinsObject);
orArray.put(termsObject);
}
request.put("or", orArray);
System.out.println("OfferMapper.getVinFromInventoryRequest " + request.toString());
return request.toString();
}
Also look what I found with a little googling :
JSONObject json = new JSONObject();
json.put("someKey", "someValue");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost request = new HttpPost("http://yoururl");
StringEntity params = new StringEntity(json.toString());
request.addHeader("content-type", "application/json");
request.setEntity(params);
httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
// handle exception here
} finally {
httpClient.close();
}
For more info see : HTTP POST using JSON in Java

Categories