I just decided to send below json data from client to server. Then i found all my previous request were not of type json. And i am unable to send json. Below is the json i want to send in data of jquery ajax.
data:{
id:"10",
sampleArr:[
{ id:"hello","sample":"hello"},
{ id:"hello1","sample":"hello1"}
]
}
and at server i get below parameters
id=10
group[0][id]=hello
group[0][sample]=hello
group[1][id]=hello1
group[1][sample]=hello1
so i am confused how to fetch all groups
One problem is that what you are sending is not valid JSON.
{ "data" : {
"id" : "10",
"sampleArr": [
{ "id" : "hello", "sample" : "hello"},
{ "id" : "hello1", "sample" : "hello1"}
]
}
}
Notice that all attribute names must be quoted, and the top-level JSON object must have curly brackets around it.
If that doesn't help, you need to explain how your servlet is receiving and parsing the JSON.
#BigMike, Thanks i am able to fetch complete json and play around with it. Was unable to send JSON even setting content type application/json. But Still checking why, but working as temperary fix (Might be that i am not using Rest API)
Related
I am consuming a Rest API and I am getting multiple Json structure as the API response for different-different use cases.
so, below are the 3 Json format structure.
1.
{
"error":"ash",
"error_description" : "gupta",
"id":"123"
}
2.
"service_response":[
{
"message":"column",
"errorCode":"Invalid_code"
}
]
3. {
"id":"ash",
"success" : "gupta",
"errors":[]
}
I am not able to write the code for different - different Json structure. I am getting parsing exception because I have handled one response. but the problem is how can we identify that which response structure we have to parse.
I am designing the rest apis that represent a file system.
File system support 3 functions
mkdir(path)
createFile(path, content) -> create if not exist and replace if exist.
readFile(path)
Here is the REST API, I am thinking of designing what do you guys think of it ?
1. mkdir
POST v1/file-system/directories
BODY {
"path" : "???"
}
RESPONSE
{
"id" : "",
"path" "",
"files": [...] // this will contain info on files or directories under this directory
}
2. createFile
PUT v1/file-system/files
BODY {
"path" : "???"
"content": ""
}
RESPONSE
{
"id" : "",
"content": ""
"path" ""
}
3. read
GET v1/file-system/files/{file-path} or
GET v1/file-system/files?file-path={file-path}
RESPONSE
{
"id" : "",
"content": ""
"path" ""
}
Can you guys tell me if these API'S are correct representation for these function.
Few questions
For GET API, shall I specify the path as path variable or query param ? If path then how will the backend differentiate between url path and file path.
e.g. v1/file-system/files/a/b/c.txt
Since create file can either create a file or replace the content of existing file, is it safe to use PUT ?
For POST and PUT, do we specify path as path variable ?
You have to understand that each request method (GET, POST, PUT...) has its own convention, but they do not differentiate a lot from each other.
For example, you could use POST to update something and not PATCH or so on.
At the end of the day, both methods, receive data in the body of the request and do something with it (or not).
Regarding your questions:
I would avoid sending a path as a query param. Send your data through the request body. That way you have a JSON and you don't have to care about specific encoding and character escaping for the URL.
Again, it is very safe since they only change because of the convention. We mostly use POST to create new data and PUT to create and replace data if it exists. Check this for more info.
Again, avoid putting paths as query params. Insert them into the body as JSON.
Read this article to learn more about HTTP Methods.
I have a problem with a very large JSON file that is too large to use ObjectMapper.readValue() into a JsonNode. I would like to use the solution from Out of memory error while parsing a large JSON using Jackson library on Android, except the JSON file is a single object with field names that are not known ahead of time, so I can't create a model POJO to deserialize to.
Each property inside the object has the same format, and I can ignore many of the properties of those inner objects (I already have a POJO class to model that). It would be easier for me to solve this problem if the JSON file was an array instead of an object. (I'm not the one creating the file, just reading from it.)
(I'm posting my solution below, but I hope there's a better one!)
Without being able to load the original file in an ObjectMapper, I decided to parse the JSON file and rewrite it as an array. Reading and writing line-by-line, I converted a file that looks like this (but much larger):
{
"Unexpected Monkey" : {
"name" : "UnexpectedMonkey",
"age" : 7
},
"Another Unexpected Name" : {
"name" : "Another Unexpected Name",
"age" : 2
}
}
into:
[
{
"name" : "UnexpectedMonkey",
"age" : 7
},
{
"name" : "Another Unexpected Name",
"age" : 2
}
]
Then I could parse the file a la How to parse a JSON string to an array using Jackson
I need to develop a program where I have to calculate driving distance and time. Getting the request to the Google Maps API works, and I get a really big response. Now I want to extract the data I need from this response. The response is in json, and the program I make is made in Java. I have no idea to do this. I tried to just put the whole response in a string, and then search the data I need, like "distance" and extract a few characters that come after that. Problem with this is, if something changes in front of it, it's all going to be at different indexes in the huge string.
A response is like this (only a bit of the big thing)
"copyrights" : "Kaartgegevens ©2017 GeoBasis-DE/BKG (©2009), Google",
"legs" : [
{
"distance" : {
"text" : "229 km",
"value" : 229411
},
"duration" : {
"text" : "2 uur 20 min.",
"value" : 8417
},
How do I easily extract the value of both "distance" and "duration"?
Basically what you want to do is serializing and deserializing the JSON to a proper Java object. You can do this by using Frameworks like Jackson or GSON to convert the JSON-String to POJOs.
This might help you as well:
How to serialize and deserialize a JSON object from Google geocode using Java
Converting JSON to Java
http://thegeekyland.blogspot.de/2015/11/serializing-and-deserializing-json-from.html
And I'm pretty sure Google has it's own API that maps the JSON internally, although I haven't used it yet. Here you might find more information:
https://developers.google.com/maps/web-services/?hl=de
Hope this helps!
I have a json file and have used simple.json jar to parse the elements. I could parse the elements successfully. But what I want is that if my json file has three elements by same name, then I want to print each name only when their index is called.
get() prints out all the elements of that name.
Please help!
Following is the json file:
{
"nodes":
[
{
"node":"1",
"ipaddr":"127.0.0.1",
"port":"8443",
"mgport":"9000"
},
{
"node":"2",
"ipaddr":"127.0.0.1",
"port":"8556",
"mgport":"9000"
},
{
"node":"3",
"ipaddr":"127.0.0.1",
"port":"8000",
"mgport":"9000"
}
]
}
I need to retrive only one port value rather than all the values.
I have successfully used the JSON Processing library to do something similar to what you describe.
Here is the link for you to have a look: https://jsonp.java.net/
Could you post also an example of the JSON you'll like to parse?
Use Jackson for parsing JSON. It will make your life easier.
This will help you.
http://www.tutorialspoint.com/jackson/jackson_first_application.htm
Thanks