i have some JSON data from server and i want to make java beans out of it.
can anybody help?
"recent_data":{[
"data_number":<data NUMBER>,
"bill_number":"<BILLING NUMBER>"
"data_date":"<data DATE>",
"due_date":"<DUE DATE>",
"data_amount":{
"amount_exchanged":"<AMOUNT EXCHANGED>"
"amount_deducted":"<AMOUNT DEDUCTED>",
"amount":"<AMOUNT>"
},
"total_amount":"TOTAL AMOUNT>",
"total_discounts":"<TOTAL_ DISCOUNTS>",
"adjusted_amount":"<ADJUSTED AMOUNT>",
"data_type":"Normal",
"data_status":"Open"
]}
You can use JSONObject to parse your json string and to populate java beans if you want that (or you could work directly with JSONObject.
It is a bit verbose, but it's built in in android.
Try Gson, it works better than JSONObject library.
Related
What im trying to do is
JSON:
{
aKey:{
aChildKey:""
},
bKey:""
}
expected:
aKey:{
aChildKey:"aKey.aChildKey"
},
bKey:"bKey"
}
Please can some one help me in getting the expected the value
You need to deserialize the JSON into an object, set the values, then serialize it back into JSON. There are a number of libraries you can use for this, like org.json, gson, or Jackson. Those libraries also allow you to modify the value directly. For example, using org.json, you can do something like this:
JSONObject jsonObject = new JSONObject(myJsonString);
jsonObject.getJSONObject("akey").put("aChildKey","aKey.aChildKey");
See How to parse JSON in Java
I Want to Return a JSON response from server to client in gRPC.
one possible way is to convert it to string return the response then convert back to Json Object in client side, but i want to know can we do better?.
i am doing some google and found we can do it with the help of google.protobuf.struct
but didn't actually find any good example.
i want an example how i can use it as JSON in java.
If you are using proto3, one option is to define a protobuf message that mirrors the JSON object you wish to populate. Then you can use JsonFormat to convert between protobuf and JSON.
Using a com.google.protobuf.Struct instead of a self-defined message can also work. There is an example shown in the similar question.
Is there any api or something to convert RSS feed to Json?
I used the api rss2json api with restTemplate, it's working fine when you map it with an entity but this one doesn't support multiple requests as it's gets overloaded plus there is no documentation for it or support so if the api goes down so is my app and I couldn't find something similar besides the rome plugin that converts to an object. I want direct conversion to Json.
I'm not sure what you mean by direct conversion to JSON, but you could simply use org.json to convert XML String to JSON String:
String xml = ..
JSONObject jObject = XML.toJSONObject(xml);
String json = jObject.toString();
More discussion here: Converting xml to json using jackson
I finished by using Rome plugin and built my own JSon structure step by step. no better solution so far.
I am new to REST , JSON and Jersey usage. Now we got a requirement where we need to implement a Client which need need to query the server with JSON request, GET and going to receive a JSON String/Object, which need to be parsed and converted into local datastructure.
Could someone help me for the below points?
What I need to download for implementing REST Client application for supporting JSON ( we have restrictions to use Jersey apis, and no other third-party apis)
Sample java client code for requesting and parsing the JSON data.
"What do I need ?", It really depends of the technology you want to use with it .. eg, if you use glassfish (netbeans server), you can use their jersey "org.glassfish.jersey.jackson.JacksonFeature" as json provider and the other ressources glassfish includes. You won't need anything else for a simple implementation.
As you are a beginner, you better follow a tutorial like this one : http://java.dzone.com/news/simple-restful-web-services
If you want to convert String to a JSONObject -
Use this library - http://www.json.org/java/
JAR file is available at http://code.google.com/p/org-json-java/downloads/list
Use below code to convert a string to JSONObject -
JSONObject final_result = new JSONObject(result);
I need to send a quite long JSON header through an http post. In Python was like this:
self.body_header = {
"client": self.client_name,
"clientRevision": self.client_version,
"uuid": str(uuid.uuid4()),
"session": self.get_sessionid()}
self.body = {
"header": self.body_header,
"country": {"IPR":"1021", "ID":"223", "CC1":"0", "CC2":"0", "CC3":"0", "CC4":"2147483648"},
"privacy": 1}
I need to do something similar in Java, ie, create somehow a JSON struct, convert it to a String and send it via http.
The question is, how can I achieve that easily? Any useful libraries? I know how to send it, but not how to build it and then create a String.
Thank you all.
You can use gson.
You can create a Java Object (POJO) and serialize it as JSON by doing:
Gson gson = new Gson();
String json = gson.toJson(yourObject);
You can then send the string over HTTP.
If you do not want to go the POJO route, you can still create the JSON struct using JsonElement, JsonArray, JsonObject in the Gson API.
I like the original org.json
i think STO had a similar discussion https://stackoverflow.com/questions/338586/a-better-java-json-library