I have some some Json string which is the return of a .Net WebAPI like this:
{ $type: "classType", "name": "someone there" }
The $type token is used by Json.Net to give a hint to (De)Serializer on what type it should use. In .net it works flawless so far...
Now we have a Java/Android client which uses GSON as the Json library.
So the problems are:
How to add $type to the serialized json string for each type?
How to make GSON to use this $type to choose to which java type to deserialize the string?
Thanks, I really appreciate any help in advice.
Best regards...
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.
I have the following JSON and I'm only interested in getting the elements "status", "lat" and "lng".
Using Gson, is it possible to parse this JSON to get those values without creating the whole classes structure representing the JSON content?
JSON:
{
"result": {
"geometry": {
"location": {
"lat": 45.80355369999999,
"lng": 15.9363229
}
}
},
"status": "OK"
}
You don't need to define any new classes, you can simply use the JSON objects that come with the Gson library. Heres a simple example:
JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(json).getAsJsonObject();
JsonObject locObj = rootObj.getAsJsonObject("result")
.getAsJsonObject("geometry").getAsJsonObject("location");
String status = rootObj.get("status").getAsString();
String lat = locObj.get("lat").getAsString();
String lng = locObj.get("lng").getAsString();
System.out.printf("Status: %s, Latitude: %s, Longitude: %s\n", status,
lat, lng);
Plain and simple. If you find yourself repeating the same code over and over, then you can create classes to simplify the mapping and eliminate repetition.
It is indeed possible, but you have to create a custom deserializer. See Gson documentation here and Gson API Javadoc here for further info. And also take a look at other reponses of mine here and here... and if you still have doubts, comment.
That said, in my opinion it is much easier for you to parse it creating the correspondent classes, even more taking into account the simplicity of your JSON response... With the usual approach you only have to write some super-simple classes, however, writing a custom deserializer, although is not that complex, it will take you probably longer, and it will be more difficult to adapt if later on you need some data else of your JSON...
Gson has a way of operating that has been designed for developers to use it, not for trying to find workarounds!
Anyway, why do you not want to use classes? If you don't like to have many classes in your project, you can just use nested classes and your project will look cleaner...
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.
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