I am getting a successful response from my jQuery GET request to my servlet. I have set the response in JSON format, but I am not able to parse that data. My JSON response is coming back in this format.
{"A":[[a1,a2],[b1,b2]]}
This is in the form of Map<List<List<String>>>.
If you see in the response the Map is A and than a upper List which contains two inner list [a1,a2] and [b1,b2].
Can someone please let me know how I can retrieve the inner list data in JSP. I could see this response in alert.
When you want to access [a1,a2] use :
data[0][0][0]
and for [b1,b2] use :
data[0][0][1]
Related
I have this Postman request.
With these headers:
Then my api is supposed to read the typeIdMap through a filter object with the #RequestBody annotation.
However when I try and read the typeIdMap
I get this error:
You can see in the logs that typeIdMap is empty. However when I change typeIdMap to be a List<Map<String,Object>> I am able to retrieve the values. Why is this?
Dont know the exact reasoning, but I figured it out...I was using org.json.JSONObject instead of org.json.simple.JSONObject. For some reason simple is the way to go when reading in the data.
I have to send some parameters for making body for requesting to the server.
like this:
"UserName":"a.m",
"CityId":"1",
"UserCategories":
[
{"CategoryId":"ab2d948a-59d1-420d-a29b-8fd88c2d637c"},
{"CategoryId":"237b6334-3c1f-44ac-bd87-a8e6be0b2144"}
]
I don't know how can I send UserCategories as an array.because it is not String that I can send it like other parameters for making body.
I should have the body like this for getting response:
body: {"CityId":"1","UserName":"a.m","UserCategories":[{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]}
but now my body is:
body: {"CityId":"1","UserName":"a.m","UserCategories":"[{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]"}
I'm really confused!
When you send or receive JSON data, it is always as a string. We might speak of JSON objects or JSON arrays, but they are really string representations of objects and arrays. You can send the data exactly as you have written here as long as you enclose the entire thing in {}. You can send just the array in string form if you wish, exactly as you typed it here. The sender should create a string from the array and the receiver should parse the JSON string into an array. There are many libraries available to help you do this in every language.
your existing UserCategories object is List/Array format after that changed in String format so only its happen
"UserCategories":"[
{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},
{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]"
this is your second-time Json, Here see your List sent with ""
like "UserCategories":"[]"
Yes. You can do that by using retrofit library. You can sent object,arraylist using Retrofit.
https://futurestud.io/tutorials/retrofit-send-objects-in-request-body.Check this url to sent Object. You can also sent arraylist under Object In body
My web clients send GET requests with URL query parameters. The receiving App can only accept POST requests with JSON body. I would like to embed a jetty servlet to the receiving App which converts GET requests to POST request, with url parameters being converted to json format body.
Input GET url for example: http://localhost:8081/?key_1=value_1&key_2=3value_2...&key_n=value_n
Expected POST json payload: {"key_1":"value_1", "key_2":"value_2", ..."key_n":"value_n"}
Could you please illustrate how to implement such functions?
I`ve worked with other programming languages, but am completely new to java. I really really appreciate your help.
Thanks and best regards,
Fischlein
You can read all the query string parameter and put it into HashMap. Then serialize this hashmap using jackson-json api or google gson api.
Jackson Reference Url :
http://wiki.fasterxml.com/JacksonHome
Read the parameters from the get request, create a json string and post it with a utility library like http://hc.apache.org/httpclient-3.x/
I have some forms and databases that will fill up some POJO's with data. Using Json (gson) I"m going to upload the data to my web server. The server will send back a responses that will eventually be placed back in objects.
My question is, should I encode/decode my objects to json and then pass strings back and forth to my WebApi class? Or should I pass my WebApi class my object and have it pass back an appropriate response object (based on the method I call.)?
So at when I call my Api that has all the http conection stuff in it, it would look something like this.
myWebApi postSomeData( jsonData ); //
Should jsonData be a pojo that will be encoded into json string inside myWebApi or should I encode it into json and pass in a string.
Or in otherwords
String postSomeData( String jsonData){
web code here..
}
or
ResponsePojo postSomeData( PostData myData){
...
myMapper.MapFrom(myData); //converts pojo to json
...
webcode
}
At some point there will be images included in the data needed to be uploaded.
I would recommend using json for data transfer, it will make your services technology independent.
You can use flexjon to serialize java objects in a pretty direct way on the server side.
For images you will need to do post http POST anyway, this should be done separately from data submit since an error in a image upload should not result in data loss. Typically you'll associate the id of your data object in the uploaded image (or reversly), so you need to insert your object to db and wait for the response to insert the dependency id to your image.
Best Regards,
Zied Hamdi
http://1vu.fr
I have two servlet: first servlet is similar to a client and creates an HttpURLConnection to call the second servlet.
I would like send a special error, formatted like a JSON object, so I call sendError method in this way:
response.sendError(code, "{json-object}")
But in the first servlet when I read error with getResponseMessage method I just get standard HTTP message and not my json object as a string.
How I can get my json string?
From the HttpServletResponse#sendError() javadoc:
The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message, setting the content type to "text/html", leaving cookies and other headers unmodified. If an error-page declaration has been made for the web application corresponding to the status code passed in, it will be served back in preference to the suggested msg parameter.
So with this approach you have no other option than extracting the message from the HTML response yourself. JSoup may however be useful in this.
To achieve what you want, you need to set the error code and write the response yourself, e.g.
response.setStatus(code);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
Instead of code you could by the way also use one of the HttpServletResponse.SC_XXX constants for this.