Kotlin Gson parsing Json Object and Array - arrays

I am pretty new to Kotlin and Json and on my work I use GSON to parse the Json.
I need to parse the following Json file into a Class by using GSON.
{
"apiKey": "blablabla",
"baseUrl": "blablabla",
"requestData": [
{
"lng": "6.971",
"lat": "50.942",
"rad": "1.5",
"type": [
"diesel",
"super"
]
},
{
"lng": "6.442",
"lat": "51.180",
"rad": "1.5",
"type": [
"diesel",
"super"
]
},{
"lng": "7.136",
"lat": "50.991",
"rad": "1.5",
"type": [
"diesel",
"super"
]
}
]
}
Now I tried to make a data class like this:
data class ApiParameterData(
var apiKey: String? = null,
var baseUrl: String? = null,
var requestData: String? = null) {
}
I also made another class to store the Json informations in it like this:
class Tankstelle: JsonDeserializer<ApiParameterData> {
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?
): ApiParameterData {
json as JsonObject
val apiKey = json.get("apiKey").asString
val baseUrl = json.get("baseUrl").asString
val requestDataJson = json.get("requestData")
val requestData = if (requestDataJson.isJsonObject) requestDataJson.asJsonObject.toString() else requestDataJson.toString()
return ApiParameterData(apiKey, baseUrl, requestData)
}
}
I tried to call it like that:
val gsonConfig = GsonBuilder().registerTypeAdapter(ApiParameterData::class.java, Tankstelle()).create()
val tanke = gsonConfig.fromJson(readJson, ApiParameterData::class.java)
println(tanke.requestData?.get(0))
But of course the output I get is "[" . I think because I get back a String or something and this is the first symbol of it?
I need to loop trough the requestData list and store it as a instance of a class and need to access each different value.
The thing is that I want to give the Json file different places and ranges it should look for gasstations. By reading the Json it should take all the pieces and create a link for each place I write in the requestData list. So in this case I would need 3 different links at the end. But this is another part I can do myself. I just don't know how to parse it so I can access and store every value in this Json.
Thank you already and have a great weekend!
If you need more informations just let me know

First you need to define two types that map to your JSON structure
class ApiParameterData(
val apiKey: String,
val baseUrl: String,
val requestData: List<RequestObject>
)
class RequestObject(
val lng: String,
val lat: String,
val rad: String,
val type: List<String>
)
Now simply parse it as
val apiData = Gson().fromJson(readJson, ApiParameterData::class.java) // No need to add TypeAdapter
// To get requestData
val requestData = apiData.requestData
requestData.forEach {
print("${it.lng}, ${it.lat}, ${it.rad}, ${it.type})
}

Related

Converting a typescript class object with dictionary to a JSON array

After some digging I decided my backend needed to consume duplicate keys and as a consequence my frontend can no longer send a dictionary as a JSON string.
See my previous question.
After applying the solution provided
let mediatagRequest = new MediaTagRequest(tags);
const headers = { 'content-type': 'application/json' }
let jsonObject = {};
for (let entry of mediatagRequest.tags.entries())
{
jsonObject[entry[0]] = entry[1];
}
const body = JSON.stringify({
tags: jsonObject
});
My current output (which is what I then wanted)
{
"tags": {
"city": "Karachi"
}
However my needs have changed and after a bit of of struggle I couldn't get my desired output to be like this
{
"tags": [
{
"key": "city",
"value": "Karachi"
},
{
"key": "city",
"value": "Mumbai"
}
]
}
Could someone help, thank you.
To get your desired output you could use the Object.entries() function to get the key, value pairs separately. This code segment will turn an object into a list of objects with key value pairs:
test_object = {
karachi: "dubai",
mumbao: "moscow",
};
output = Object.entries(test_object).map(([key, value]) => ({ key, value}));
console.log(output);
You can adapt this code to select the desired parts of your object and format them as you like. There are other Object functions you can see in the documentation.

How to parse JSON response which contains nested JSON array

I'm trying to parse a JSON response from an API and store the data to DATA CLASS and sending the data to recycler adapter as ArrayList.
The JSON Array has another array of objects inside, and I'm not able to find a way to properly parse that JSON response.
Here is my data class:
data class OrderDetails (
val orderId: String, // order_id value from json object goes here //
val restaurantName: String, // restaurant_name value from json object goes here //
val totalCost: String, // total_cost value from json object goes here //
val orderDate: String, // order_placed_at value from json object goes here //
val orderFoodDetails: String // food_items value in json response is an array and i'm stuck here //
)
Here is my Kotlin code:
try {
val data = it.getJSONObject("data")
val success = data.getBoolean("success")
if (success) {
val arrayData = data.getJSONArray("data")
for (i in 0 until arrayData.length()) {
val orderJsonObject = arrayData.getJSONObject(i)
val orderObject = OrderDetails(
orderJsonObject.getString("order_id"),
orderJsonObject.getString("restaurant_name"),
orderJsonObject.getString("total_cost"),
orderJsonObject.getString("order_placed_at"),
orderJsonObject.getJSONArray("food_items").toString() // getting array and storing as a string
)
orderList.add(orderObject)
for (orders in orderList) {
val foodData = orders.orderFoodDetails
val jsonFood = JSONArray(foodData)
for (j in 0 until jsonFood.length()) {
val foodJsonObject = jsonFood.getJSONObject(j)
val foodObject = OrderFoodDetails(
foodJsonObject.getString("food_item_id"),
foodJsonObject.getString("name"),
foodJsonObject.getString("cost")
)
ordersFood.add(foodObject)
}
}
}
Here is the Json response:
{
"data": {
"success": true,
"data": [
{
"order_id": "17790",
"restaurant_name": "Rotten Tomatoes",
"total_cost": "280",
"order_placed_at": "02-11-20 19:00:54",
"food_items": [
{
"food_item_id": "156",
"name": "Rotten Bhajiya",
"cost": "100"
},
{
"food_item_id": "155",
"name": "Rotten Salad",
"cost": "100"
},
{
"food_item_id": "154",
"name": "Rotten Soup",
"cost": "80"
}
]
},
Required Output
Prefered Output
My output
my current output
do you tried to use GSON or Moshy lib ?
This case will be easier solved with one of this :)
If you don't want to use one of them, just try to replace your for cycle to map or for each, to make it more readable. Looks like you make it right, but can you check the result of try catch block please?

Read from JSON or XML file and write into array of Core Data Objects

And I have created a Core Data Entity ("Exstand") that looks like the below JSON structure (same fields) and I try to read from the JSON file and write into an array of Core Data objects.
I have a local (stored in Xcode) JSON file that contains data looking like this:
[{
"objectid": 13003,
"lat": 40.198539203831054,
"long": 20.294164128143816,
"adresse": "1 Random Street
"zeitraum": "v. 7-20h",
"stellplatzanzahl": 3
},
{
"objectid": 13004,
"lat": 50.25018761410509,
"long": 30.44382262875748,
"adresse": "2 Random Street",
"zeitraum": "",
"stellplatzanzahl": 6
}]
I have went through many tutorials and posts here but can't for the life of me get it working for me because they either open a remote file and/or don't try to store into Core Data.
This is the code I have so far and it crashes when I try append the object to the array. When the .append line is commented I can see in the console that it doesn't actually write data into "stand".
class EventDetails: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var eventMap: MKMapView!
var annotation:MKAnnotation!
var Standarray: [Exstand] = []
var stand:Exstand? = nil
override func viewDidLoad() {
super.viewDidLoad()
do {
let data = NSData(contentsOf: url!)
let jsonData = try JSONDecoder().decode([Nstand].self, from: data! as Data)
for detail in jsonData {
self.stand?.adresse = detail.adresse as String
self.stand?.lat = detail.lat as Double
self.stand?.long = detail.long as Double
self.stand?.stellplatzanzahl = Int16(detail.stellplatzanzahl as Int)
self.stand?.zeitraum = detail.zeitraum as String
print(stand?.adresse)
print("adressen!")
//The line below crashes the app Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Standarray.append(stand!)
}
print("array!")
print(Standarray)
} catch {
print(error) // Handle Error
}
}
}
I also created this struct because in some tutorials they worked with that instead of Core Data entities, so if that is a better, I can also use that.
struct NStand : Codable{
var objectid: Int
var lat: Double
var long: Double
var adresse: String
var zeitraum: String
var stellplatzanzahl: Int
}
I also have the JSON data in a XML file. I don't care from where to read it, all I want is my app to actually read and store the data.
Thanks in advance for any helpful advice.
Your json is invalid here
"adresse": "1 Random Street
to
"adresse": "1 Random Street",
should be
[{
"objectid": 13003,
"lat": 40.198539203831054,
"long": 20.294164128143816,
"adresse": "1 Random Street",
"zeitraum": "v. 7-20h",
"stellplatzanzahl": 3
},
{
"objectid": 13004,
"lat": 50.25018761410509,
"long": 30.44382262875748,
"adresse": "2 Random Street",
"zeitraum": "",
"stellplatzanzahl": 6
}
]
struct NStand : Codable{ // for core-data NSManagedObject
let objectid,stellplatzanzahl: Int
let lat,long: Double
let adresse,zeitraum: String
}
let str = """
[{
"objectid": 13003,
"lat": 40.198539203831054,
"long": 20.294164128143816,
"adresse": "1 Random Street",
"zeitraum": "v. 7-20h",
"stellplatzanzahl": 3
},
{
"objectid": 13004,
"lat": 50.25018761410509,
"long": 30.44382262875748,
"adresse": "2 Random Street",
"zeitraum": "",
"stellplatzanzahl": 6
}]
"""
do {
let res = try JSONDecoder().decode([NStand].self, from: Data(str.utf8))
print(res)
}
catch {
print(error)
}
The error occurs because stand is nil and never gets created.
Adopting (De)codable in a NSManagedObject subclass is a bit tricky because you have to call the designated initializer which requires the managed object context.
Based on this great answer by casademora first create an extension of CodingUserInfoKey and JSONDecoder to be able to pass the managed object context along.
extension CodingUserInfoKey {
static let context = CodingUserInfoKey(rawValue: "context")!
}
extension JSONDecoder {
convenience init(context: NSManagedObjectContext) {
self.init()
self.userInfo[.context] = context
}
}
Then add CodingKeys and the required initializer to your NSManagedObject subclass (in the class, not in the extension).
class Exstand: NSManagedObject, Decodable {
private enum CodingKeys: String, CodingKey { case objectid, lat, long, adresse, zeitraum, stellplatzanzahl }
public required convenience init(from decoder: Decoder) throws {
guard let context = decoder.userInfo[.context] as? NSManagedObjectContext else { fatalError("Exstand Error: no managed object context!") }
let entity = NSEntityDescription.entity(forEntityName: "Exstand", in: context)!
self.init(entity: entity, insertInto: context)
let values = try decoder.container(keyedBy: CodingKeys.self)
let objectid = try values.decode(String.self, forKey: .objectid) // do something with objectid
adresse = try values.decode(String.self, forKey: . adresse)
lat = try values.decode(Double.self, forKey: .lat)
long = try values.decode(Double.self, forKey: .long)
zeitraum = try values.decode(String.self, forKey: .zeitraum)
stellplatzanzahl = try values.decode(Int16.self, forKey: .stellplatzanzahl)
}
...
}
Thats all you need, now you can decode the JSON directly into Core Data
var standArray = [Exstand]()
override func viewDidLoad() {
super.viewDidLoad()
do {
let data = try Data(contentsOf: url!)
let context = // here add the reference to the managed object context
let decoder = JSONDecoder(context: context)
standArray = try decoder.decode([Exstand].self, from: data)
try context.save()
} catch {
print(error) // Handle Error
}
}
The other structs / classes are not needed.

Angular Update value in localStorage

The problem I'm having, I can't add/change a given item inside a JSON object. I tried to convert it to use a simple put but that doesn't work since: Property "put" does not exist in type of "JSON"
Key: 123 Value:{"name":"123","email":"123","password":"123","country":"123","about":"123","image":""}
My method to change the country value.
newJson:JSON;
onSubmit(value:any){
this.newJson = JSON.parse(localStorage.getItem(this.id));
this.newJson.put("country", value.country);
localStorage.setItem(JSON.stringify(this.newJson));
}
The value I get from the parameter (the submitted value)
{"country":"Portugal"}
Try like this :
export class Component {
private jsonObj: any = {};
private key: string = "123";
constructor(){
this.jsonObj = {
"name": "123",
"email": "123",
"password": "123",
"country": "123",
"about": "123",
"image": ""
}
localStorage.setItem(this.key, JSON.stringify(this.jsonObj));
}
ngOnInit() {
let localStorageJsonData = JSON.parse(localStorage.getItem(this.key));
localStorageJsonData.country = "france";
localStorage.setItem("obj", JSON.stringify(localStorageJsonData));
console.log('localStorageJsonData', localStorageJsonData);
}
}

How to read json response using jsonArray in java code

I have a query, I have to read key "class" under key"studentData" from the below mentioned json response.
{
"studentData": [
{
"name": "john",
"class": 2,
"rollno": "2015"
}
],
"yearofenrollment": 2017
}
Please help.
Thanks in advance
JSONObject jsonObjectResponse = new JSONObject(response.toString()); //obtain the object
JSONArray jsonMainNode = jsonObjectResponse.optJSONArray("studentData");//get array from object
JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);//get first object in array
String studentDataValue = jsonChildNode.optString("class");//obtain value from class key

Resources