It's my first time dealing with JSON in android studio, and I'm trying to parse the response from google books API to JSON and then retrieve some info about the volume such as the title, author, and the description. the problem is there are JSON objects within the main JSON. Should I create a data class for each JSON?
the API link 'https://www.googleapis.com/books/v1/volumes?q=kotlin'
how i'm parsing it
val jsonResponse = gson.fromJson(body, JsonResponse::class.java)
the data classes that i created
data class JsonResponse
(val kind:String,val count:String,val items:List<JsonItems> )
data class JsonItems
(val kind:String,
val id:String,
val etag:String,
val self:String,
val volumeInfo:List<VolumeInfo>,
val saleInfo:List<SaleInfo>,
val accessInfo:ListList<AccessInfo> ,
val searchInfo:List<SearchInfo>)
is there any simpler solution to avoid unused data classes?
You need to have these classes to be able to parse the json, but then you can just create objects of other classes using those data (and drop the earlier instances).
Also, you don't need to include fields that you don't need.
You can parse without data classes and you can use other tools than Gson.
I had a use case similar to yours. I did not want to write data classes, but just get direct access to the specific data I was interested in from a larger nested JSON object.
I found a solution that worked well for me here
https://johncodeos.com/how-to-parse-json-in-android-using-kotlin/
using JSONTokener https://developer.android.com/reference/org/json/JSONTokener
Based on these examples, some code for parsing books might look like:
// response is a String with the JSON body
val jsonObject = JSONTokener(response).nextValue() as JSONObject
val items = jsonObject.getString("items") as JSONArray
for (i in 0 until items.length()) {
val volumeInfo = items.getJSONObject(i).getString("volumeInfo")
val title = volumeInfo.getString("title")
}
Another Stackoverflow post with other suggestions is here How to parse JSON in Kotlin?
Related
I am trying to parse the results of an API call which returns a unique first property.
{
"AlwaysDifferent12345": {
"fixedname1" : "ABC1",
"fixedname2" : "ABC2"
}
}
I am using retrofit2 and jackson/gson and cannot figure out how to cope with dynamic property names within the retrofit2 framework. The following works fine
data class AlwaysDifferentDTO(
#JsonProperty("AlwaysDifferent12345") val alwaysDifferentEntry: AlwaysDifferentEntry
)
I have tried
data class AlwaysDifferentDTO(
#JsonProperty
val response: Map<String, AlwaysDifferentEntry>
)
But this returns errors Can not instantiate value of type... The return value from the API is fixed i.e. map<string, object>.
I have read you can write a deserializer but it looks like I need to deserialize the whole object when all I want to do is just ignore the string associated with the response.
I have read
https://discuss.kotlinlang.org/t/set-dynamic-serializedname-annotation-for-gson-data-class/14758
and several other answers. Given unique properties names are quite common it would be nice to understand how people deal with this when using retrofit2
Thanks
Because the JSON doesn't have a 1-to-1 mapping Jackson can't map it automatically using annotations. You are going to need to make your own Deserializer.
In this tutorial you can learn how to create your own custom Deserializer for Jackson. https://www.baeldung.com/jackson-deserialization
In the tutorial you will see the first line under the deserialize function is
JsonNode node = jp.getCodec().readTree(jp);
using this line you can get the JSON node as a whole and once you have it you can call this function
JsonNode AlwaysDifferent12345Node = node.findParent("fixedname1");
Now that you have that node you can retrieve its value like shown in the rest of the tutorial. Once you have all the values you can return a new instance of the AlwaysDifferentDTO data class.
I am facing a problem when trying to send an Http post request to the backend of my application. I am trying to send a post request like this :
{"a":[{"data":"https://news.google.com/rss/search?q=Pallini&hl=el"}]}
and instead it is being send something like this:
{"a":[{"data\":\"https://news.google.com/rss/search?q=Pallini&hl=el"}]}
or:
{"a":[{\"data\":\"https://news.google.com/rss/search?q=Pallini&hl=el\"}]}
So, I have a list that contains strings and every time I add the string that I want it to be shown in the json array, the code is like this:
var arrayListForA: ArrayList<JsonElement>? = arrayListOf()
config.forEach {
arrayListForA?.add(it)
}
The config is an another list that contains the jsons object as strings.
My question is, if there is any way to create the http post request body in Kotlin with the use of classes, objects etc, in a more automated way ?! for example, instead of a list with strings, I could use a list with Data class objects.
val dataList : ArrayList<Data> = arrayListOf()
where Data class is :
#Parcelize
data class Data(
#Expose #SerializedName("data") val data: String?
) : Parcelable
Is there any solution/idea to send the body of the post request as I need it ?
You can use retrofit and okhttp for this in Android. Retrofit helps you deal with networking easily. Also you will be able to post a custom data model as body in the api request. The interface will look something like given below. You can read more about retrofit here. retrofit
#POST(Urls.PURCHASE)
fun purchase(#Body purchaseAddonReqModel: PurchaseReqModel):Single<BaseResponse<EmptyResponse>>
Here you can add your custom model by adding the #Body annotation
I need to parse only one list of objects from JSON object with Retroft.
Here's my JSON file
I need only "similar" array from it. Also I've already created POJO for elements of this array
Is there a way to make this without making POJO for the rest of the JSON file?
Just make pojo with necessary fields:
data class ServerResponse(
val recipe: RecipeResponse
)
data class RecipeResponse(
val similar: List<RecipeBrief>
)
I am parsing XML response using SimpleXmlConverterFactory which works perfect but it gives me data model classes with respect to provided model class while making API call but now I need a response string as well which I got from the server.
I have tried by taking responseBody in the model class but due to that it gives error in my XML parsing
class ResponseAndPojo(val value : MODEL, val response : ResponseBody)
Using the above class I am trying to parse response with a retrofit API call but it is not working
I have achieved this by using SimpleXmlConverterFactory below is my code which will use XML response body and convert it to data model class so that I got both XML response string and model class
val strategy = AnnotationStrategy()
val serializer = Persister(strategy)
val model = SimpleXmlConverterFactory.create(serializer).responseBodyConverter(CustomModel::class.java,arrayOfNulls<Annotation>(0),null)?.convert(response) as CustomModel
I was wondering if somewhere out there exists a java library able to query a JSONObject. In more depth I'm looking for something like:
String json = "{ data: { data2 : { value : 'hello'}}}";
...
// Somehow we managed to convert json to jsonObject
...
String result = jsonObject.getAsString("data.data2.value");
System.out.println(result);
I expect to get "hello" as output.
So far, the fastest way I have found is using Gson:
jsonObject.getAsJsonObject("data").getAsJsonObject().get("data2").getAsJsonObject("value").getAsString();
It's not actually easy to write and read. Is there something faster?
I've just unexpectedly found very interesting project: JSON Path
JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document.
With this library you can do what you are requesting even easier, then my previous suggestion:
String hello = JsonPath.read(json, "$.data.data2.value");
System.out.println(hello); //prints hello
Hope this might be helpful either.
While not exactly the same, Jackson has Tree Model representation similar to Gson:
JsonNode root = objectMapper.readTree(jsonInput);
return root.get("data").get("data2").get("value").asText();
so you need to traverse it step by step.
EDIT (August 2015)
There actually is now (since Jackson 2.3) support for JSON Pointer expressions with Jackson. So you could alternatively use:
return root.at("/data/data2/value").asText();
First of all, I would recommend consider JSON object binding.
But in case if you get arbitrary JSON objects and you would like process them in the way you described, I would suggest combine Jackson JSON processor along with Apache's Commons Beanutils.
The idea is the following: Jackson by default process all JSON's as java.util.Map instances, meanwhile Commons Beanutils simplifies property access for objects, including arrays and Map supports.
So you may use it something like this:
//actually it is a Map instance with maps-fields within
Object jsonObj = objectMapper.readValue(json, Object.class);
Object hello = PropertyUtils.getProperty(jsonObj, "data.data2.value")
System.out.println(hello); //prints hello
You can use org.json
String json = "{ data: { data2 : { value : 'hello'}}}";
org.json.JSONObject obj = new org.json.JSONObject(json);
System.out.println(obj.query("/data/data2/value"));
I think no way.
Consider a java class
class Student {
Subject subject = new Subject();
}
class Subject {
String name;
}
Here if we want to access subject name then
Student stud = new Student();
stud.subject.name;
We cant access name directly, if so then we will not get correct subject name. Like here:
jsonObject.getAsJsonObject("data")
.getAsJsonObject()
.get("data2")
.getAsJsonObject("value")
.getAsString();
If you want to use same like java object then use
ClassName classObject = new Gson().fromJson(JsonString, ClassName.class);
ClassName must have all fields to match jsonstring. If you have a jsonobject inside a jsonobject then you have to create separate class like I'm doing in Student and Subject class.
Using Java JSON API 1.1.x (javax.json) one can make use of new JavaPointer interface. Instance implementing this interface can be considered to some extend as kind of XPath expression analog (see RFC-6901 for details). So in your case you could write this:
import javax.json.*;
//...
var jp = Json.createPointer("/data/data2/value");
System.out.println(jp.getValue(jsonObject));
In 1.1.4 version of JSON there's also nice addition to JsonStructure interface (which is implemented by JsonObject and JsonArray), namely getValue(String jsonPointer). So it all comes down to this simple one-liner:
System.out.println(jsonObject.getValue("/data/data2/value"));