i'm new in programming kotlin and i'm writing a Android App for the Google Glass EE 2.
The plan ist to create a App for bicycle navigation with osmdroid just for fun.
The Map is running fine and drawing polylines, polygons and markers works great. Downloading data via Coroutine is also working, my biggest problem is now to parse the JSON data via Kotlin Serialization.
My first Problem is that the Keys (Example: "13_8593_5244_2_8_100") inside map are not static.
In my opnion i need something like a array to call all elements with a for loop but i don't know how to write this as a #Serializable Class.
My second Problem is that the arrays in "gameEntities" have different structures but i think i can solves this in the way described here: Link Stackoverflow
Is this the right way?
Hope you can help me, thanks a lot!
Here the shorte JSON:
{
"result":{
"map":{
"13_8593_5244_2_8_100":{
"gameEntities":[
[
"276572523a5f439cb97d9dec9a5df345.9",
1660584038728,
[
"e",
"R",
"11b6cd618b6e4610a6b02e475dd7be89.16",
52553403,
13366757,
"0bd7e7b363ad428eae5f4d6c6cc0b85f.16",
52550446,
13384642
]
],
[
"527f2069da8444bbb6e01d5c6a7982ea.b",
1660968825455,
[
"r",
"R",
[
[
"2dfe2da57f2a45a68b3778a3e3c966da.16",
52557237,
13373585
],
[
"bc64b427d9654161a9520ce5db4f8d5d.16",
52559693,
13353633
],
[
"890ec73c6d464b6080b66d4f025b3704.16",
52557728,
13350805
]
]
]
]
]
},
"13_8594_5243_2_8_100":{
"gameEntities":[
[
"276572523a5f439cb97d9dec9a5df345.9",
1660584038728,
[
"e",
"R",
"11b6cd618b6e4610a6b02e475dd7be89.16",
52553403,
13366757,
"0bd7e7b363ad428eae5f4d6c6cc0b85f.16",
52550446,
13384642
]
],
[
"13c70bd6dc3742949184388c3520f3b2.9",
1660584025873,
[
"e",
"R",
"11b6cd618b6e4610a6b02e475dd7be89.16",
52553403,
13366757,
"f41e703ec4c346219af94462cbe7afb3.16",
52551164,
13379923
]
]
]
},
"13_8593_5246_2_8_100":{
"error":"TIMEOUT"
}
}
}
}
The Kotlin Code is:
#Serializable
class EntitieTop(val result: EntitieResult)
#Serializable
class EntitieResult(val map: Array<EntitieMap>)
#Serializable
class EntitieMap(val gameEntities: List<EntitieGameEntities>)
#Serializable
class EntitieGameEntities(val guid: String, val timestamp: Long)
fun serialTest() {
val data = Json.decodeFromString<EntitieTop>(jsonString)
println(data.result.map[0])
}
Instead of trying to reinvent the wheel, try using an existing json serialization library. One of the easiest ones is GSON. Here's an example for using it
Related
I am an android Novice.
I am using the Universal Android Media Player sample code as basis of another app but I cannot figure out how to update the default music JSON.
The code is in Kotlin. Any help in figuring out where the JSON is and how to update it will be much appreciated?
The UAMP sample code uses an external JSON file with this format:
{
"music": [
{
"id": "wake_up_01",
"title": "Intro - The Way Of Waking Up (feat. Alan Watts)",
"album": "Wake Up",
"artist": "The Kyoto Connection",
"genre": "Electronic",
"source": "https://storage.googleapis.com/uamp/The_Kyoto_Connection_-_Wake_Up/01_-_Intro_-_The_Way_Of_Waking_Up_feat_Alan_Watts.mp3",
"image": "https://storage.googleapis.com/uamp/The_Kyoto_Connection_-_Wake_Up/art.jpg",
"trackNumber": 1,
"totalTrackCount": 13,
"duration": 90,
"site": "http://freemusicarchive.org/music/The_Kyoto_Connection/Wake_Up_1957/"
},
...
]
}
This variable is declared inside the MusicService.kt
private val remoteJsonSource: Uri = Uri.parse("https://storage.googleapis.com/uamp/catalog.json")
So what i partially achieved was to set an app that gets JSON text from a weather website of this form:
{
"base": "stations",
"clouds": {
"all": 20
},
"cod": 200,
"coord": {
"lat": 40.94,
"lon": 24.41
},
"dt": 1513711200,
"id": 735861,
"main": {
"humidity": 100,
"pressure": 1027,
"temp": 274.15,
"temp_max": 274.15,
"temp_min": 274.15
},
"name": "Kavala",
"sys": {
"country": "GR",
"id": 5684,
"message": 0.0039,
"sunrise": 1513662178,
"sunset": 1513695412,
"type": 1
},
"visibility": 10000,
"weather": [
{
"description": "few clouds",
"icon": "02n",
"id": 801,
"main": "Clouds"
}
],
"wind": {
"deg": 51.0029,
"speed": 3.07}}
decodes it into a list and store the results in some labelboxes in the app.The values i want to use from this JSON are:
("main": {"temp":}, "weather": {"main":}, "main": {"humidity":}, "wind":{"speed}".
I have managed to that quite efficiently using the integrated json decode function and indexing from App Inventor.
Photo of Block Code:
(SCREEN RED POINTER is supposed to be "weather": {"main":} value)
My problem is that for some cities(the app searches for data from the city name the user enters) the indexes for "weather": {"main":} and "wind":{"speed}" are different.Is there any way i can set up a check routine for this problem?
For example, for "weather": {"main":} the usual indexing as it can be seen in my code(image) is 11 2 1 4 2 of the created list.For the cities that have different index and respond with an error i think its 10 2 1 4 2.
OBJECTIVE:So what i want to do is find a way to check if the element on the 11 2 1 4 2 exists so i can use it,else look for the element with indexes 10 2 1 4 2 to use.
UPDATE:OBJECTIVE COMPLETE DESPITE THE JSON deformity 2 dependent look up in pairs fixed the issue!
Don't use nested select list item blocks as you are doing currently...
The better approach is to use the loopup in pairs block and the sequence of the data will not be important anymore...
How does the lookup in pairs block work?
Further links
JSON and list of lists: example1 and example2
Easy to decode large information with JSON format by Carlos
A general purpose JSON browser routine by ABG
JSON Tools Extension by LukeGackle
I have a source for tiles as an url and want to add these to my map.
I am able to do this with Google Map and OSMDroid, but I do not know how to figure it out using Mapbox.
My url has the format "http...mysource..x=..y=..z=.."
I have seen a solution for web, but I do not find such an approach for mobile.
I assume you have a URL for a tile server such as http://server/tiles/{z}/{x}/{y}.png If so, please update your question.
Please see this Mapbox example, https://www.mapbox.com/android-sdk/examples/custom-raster/ to add in a custom Mapbox Style. Note the parameter for setStyleUrl. Open up that json file and inspect it.
mapView.setStyleUrl("https://www.mapbox.com/android-sdk/files/mapbox-raster-v8.json");
You will then need to create two JSON files. See this project (which is for iOS, but the JSON files are identical for Android, Web, and iOS.).
tile.json sample
{
"name": "geography-class",
"version": "1.0.0",
"description": "",
"type": "overlay",
"format": "png",
"minzoom": 0,
"maxzoom": 8,
"bounds": [-117.30596604, 32.78617375, -117.21820077, 32.88817706],
"scale": "1",
"profile": "mercator",
"tiles": ["http://server/tiles/{z}/{x}/{y}.png"],
"tilejson": "2.0.0",
"scheme": "xyz"
}
Mapbox Style JSON, put this in the parameter for setStyleUrl()
{
"version": 8,
"sources": {
"yourTileLayer": {
"url": "http://server/tiles/tile.json",
"type": "raster",
"tiles": [
"http://server/tiles/{z}/{x}/{y}.png"
],
"tileSize": 256
}
},
"layers": [
{
"id": "yourTileLayer",
"type": "raster",
"source": "yourTileLayer"
}
]
}
I am writing a webapp in flask. That gets a table from a MySQL server on a raspberry pi.
INITIALY TESTED WITH POSTMAN
I set the encoding in MySQL workbench to utf-8 bin when creating the table.
I set the charset=utf8 in MySQLdb when using MySQLdb.connect(charset=utf-8)
The original json looks like this.
{
"Menu": [
[
1,
"ΣΑΛΑΤΑ",
"ΚΡΗΤΙΚΗ",
5.5
],
[
2,
"ΣΑΛΑΤΑ",
"ΦΑΚΗ",
6
]]
}
This is the return json (POSTMAN)
{
"Menu": [
[
1,
"ΣΑΛΑΤΑ",
"ΚΡΗΤΙΚΗ",
5.5
],
[
2,
"ΣΑΛΑΤΑ",
"ΦΑΚΗ",
6
]]
}
SO FAR IN POSTMAN IT WORKS JUST FINE
NOW WHEN I WANT TO print it on the webpage it appears as this:
{
"Menu": [
[
1,
"\u03a3\u0391\u039b\u0391\u03a4\u0391",
"\u039a\u03a1\u0397\u03a4\u0399\u039a\u0397",
5.5
],
[
2,
"\u03a3\u0391\u039b\u0391\u03a4\u0391",
"\u03a6\u0391\u039a\u0397",
6.0
]
]
}
AND ALSO WHEN I POST FROM THE ANDROID APPLICATION THE SAME ISSUE.
to return the json i am using jsonify.
That is valid JSON, with unicode characters escaped so that the value is ASCII.
If you actually parse the JSON in JavaScript, you will still get the correct values. Assuming you called your endpoint with an AJAX request, you would load the data in the callback with JSON.parse. Some AJAX libraries may handle this for you.
var parsed_data = JSON.parse(data);
You can stop jsonify from escaping everything to ASCII by disabling JSON_AS_ASCII. You should avoid doing this, since this can result in invalid characters in the response.
app.config['JSON_AS_ASCII'] = False
Preferrably, leave the data as is and use it from JavaScript.
I'm thinking of using couchDB in one of my android applications. I've designed a basic application looking into couchdb tutorial. Couchbase server has been setup. When I replicate the data, I can see 3 documents per replication.
1. 78e2c7dc-da9a-42f4-95a6-03bd6cea7b6e { "_sync": { "rev":
"1-5aca82e9-e4cc-47db-9cce-fe32fea68d1c", "sequence": 1, "history":
{ "revs": [ "1-5aca82e9-e4cc-47db-9cce-fe32fea68d1c" ], "parents": [
-1 ], "bodies": [ "" ], "channels": [ null ] }, "time_saved": "2014-04-26T14:58:37.399871861+05:30" }, "check": false,
"created_at": "25-04-14", "text": "huhahhahhahah" }
Delete Edit Document
_sync:local:a7e278f189bb5954e0bae030cf2dd2b231a7318e { "_rev": "0-1", "lastSequence": "1" }
Delete Edit Document
_sync:seq 4
I need to know how to filter this meta data from getting replicated. All I need from the above data is the "check, created_at, text" attributes. I did some research. All I could found is that I've to ignore _sync properties during sync. But I could not get how to do this.