Below is the model class I am created for my json response :
data class MyModelClass(
val one: String,
val two: String,
val three: String,
val four: Int,
val tempCurrentCharge: List<CurrentCharge>,
val tempCurrentDischarge: List<CurrentDischarge>
)
Now, above you can see that I am getting Arrays of List<CurrentCharge> and List<CurrentDischarge> in my Json response as below :
"tempCurrentCharge": [
{
"temp": -600,
"cRating": 0
},
{
"temp": 0,
"cRating": 10
}
]
Now, I can successfully parse the json response and saved it in my local db (as per my need).
Now I have two feilds in my local db table, one is for CurrentCharge and another if for CurrentDischarge.
I have to save the whole json string as value in this.
Currently It saved as Object.toString() Instead I want to save the whole json string which is as shared above.
The issue is since I have created the pojo class for json parsing, Its parsing the data for tempCurrentCharge and tempCurrentDischarge automatically.
But I want to store the values of json String in it.
So, What I have done is: changed the type of both the variables as String
But then it given me type casting error as :
"BEGIN_ARRAY... found String".
So, Anyone please suggest me how can I achive storing above jsonArray as string json in my local room db field?
Thanks in Advance.
Let your data class remain as it is, the error you are receiving is justified because in the response you are getting an array while you have changed the type in your data class to a String and thus the following error
"BEGIN_ARRAY... found String".
Coming to what you need, you can simply convert it back to JSON once it is parsed and save it then. You will need Gson library and I am sure it is added to your project. If not please add it.
What next you will need is to simply do is this. I am assuming you have an object of the BatteryConfigurationDetails class
val batteryConfig = BatteryConfigurationDetails()
val tempCurrentChargeJson = Gson().toJson(batteryConfig.tempCurrentCharge)
val tempCurrentDischargeJson = Gson().toJson(batteryConfig.tempCurrentDischarge)
To convert it back you can use the following function
Gson().fromJson()
Add the following if you do not have the library already
implementation 'com.google.code.gson:gson:2.8.7'
Related
I am getting JSON response from the API call. But in that response one of the JSONArray is coming as JSONObject if there is no data as given below. Without data : "InbuildData": null.
With data :"InbuildData": [{"value": "Yes","checkStatus":0}]. Anyone please suggest better option to identify this situation!
Not user about your confusion this is a simple case you can handle this like this in your model
#SerializedName("InbuildDataItem")
var inBuildDataItem:InbuildDataItem?=null
data class InbuildDataItem(
#SerializedName("checkStatus")
val checkStatus: Int,
#SerializedName("value")
val value: String
)
Before using check if inBuildDataItem is null or not
code is present inside this link https://github.com/Pranjul120568/AndroidPracticeQuakeReport. and link to the api https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ci39540463.geojson
You have to use Response instead of Property at parsing
Problem at line number 31 :
val quake=gson.fromJson(response, Properties::class.java)
Solution:
val quake = gson.fromJson(response, Response::class.java)
In Android application development using Kotlin, which would be the efficient way to sort a JSON array which got objects having dates as property values.
I want to sort my response with date and pick the latest created item. For example, my list response is
[
{
name : "Joseph"
enrolled_at : "2019-12-17 14:16:51"
},
{
name : "Infant"
enrolled_at : "2019-12-20 10:06:22"
},
{
name : "Raj"
enrolled_at : "2020-02-10 07:19:16"
}
]
I want to sort this with "enrolled_at" property date to get the recent enrolled item. The original response is huge and so I cannot get the real response here.
What would be the efficient way to sort the dates using Kotlin. Tried with sortedWith and other collections in Kotlin. Looking for suggestions.
First, you need to create the method that converts that String into a Date in order for your array to be sortable.
And assuming you're converting that JSON to a list of users of type:
data class User(val name: String, val enrolled_at: String)
Using the ability to create extensions in Kotlin, you can create the following String method extension:
fun String.toDate(): Date{
return SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).parse(this)
}
Then you can sort the user's list by ascending date by doing:
users.sortedBy { it.enrolled_at.toDate() }
I'm a newbie in Android. I'm making a crypto currency tracker. I'm using a Korean exchange API. But, when I read JSON data as String, it's a little different from what I found in literature.
In the red bracket, there are some data but are classified as the String like "BTC, ETH". And I learned in a book like this
String timestamp;
String payment_currency; // payment currency(USD, KRW...)
ArrayList<BidAskBithumb> bids = new ArrayList<BidAskBithumb>(); // Bids
ArrayList<BidAskBithumb> asks = new ArrayList<BidAskBithumb>(); // Asks
Every property is saved in String and Arrays. But in that JSON data, there is currency's name like BTC or ETH after payment_currency String. I don't know I have to make every class file like BTC.java or ETH.java. How can I solve this?
This is the JSON response I'm getting.
I read all the currency's data:
The original API documentation link.
I don't know I have to make every class file like BTC.java or ETH.java. How can I solve this?
No, you dont have to create new class files for them, you need to create model class according to your json file. & handle its behaviour.
You can try this to create your model.
I have an XML web service. I want to parse this XML and I want to store in an separate textviews. The following is an XML content, and I have finished getting it in a String variable.
{
"30_year_rate": 4.25,
"30_year_pi": 196.78,
"30_year_apr": 4.375,
"20_year_rate": 4.375,
"20_year_pi": 250.37,
"20_year_apr": 4.5,
"15_year_rate": 3.75,
"15_year_pi": 290.89,
"15_year_apr": 3.875,
"10_year_rate": 3.625,
"10_year_pi": 397.89,
"10_year_apr": 3.75,
"5_year_arm": 2.75,
"5_year_pi": 163.3,
"5_year_apr": 2.875,
"7_year_arm": 3,
"7_year_pi": 168.64,
"7_year_apr": 3.125,
"3_year_arm": 4,
"3_year_pi": 190.97,
"3_year_apr": 4.125,
"5_year_io_arm": "N/A",
"5_year_io_pi": "N/A",
"5_year_io_apr": "N/A",
"QuoteID": 1449,
"Licensed": "N"
}
How can I parse this data? I want to convert it to a JSON object and retrieve it, or any other reasonable approach.
If what you're getting back from the webservice is the string above, then you already have a JSON string. To create an object that can retrieve information from it, use something like JSONObject.
JSONObject object = new JSONObject(your_string_variable);
double thirtyYearRate = object.getDouble("30_year_rate");
String licensed = object.getString("Licensed");
etc.
You might (will) run into some issues where you try to pull a double from a JSON field that contains a string; i.e., the "N/A" fields above. You'll likely have to pull them out as strings and then try to parse doubles from them, and if the parsing throws an exception, you'll know it's a string.
Alternately, you could look into JSON binding with something like Jackson, which apparently runs on Android.
To parse JSON, you could use the built in JSONObject org.json Or Json-lib if you use an old version of android.
To parse XML, use XMLPullParser. A sample can be found here: Parsing XML Data