How to insert nested json into SQLite Flutter - android

I want to make a categories screen, the backend returns a nested json with 4 trees in total if I'm not mistaken,
example:
when I press the most general tree of the categories, I would like it to be displayed as a dropdown and there, put the children of this category, the ones that come in the json clearly, hope to explain myself well.
I have to fill these widgets, their text or titles with the text that comes clearly in the json, but I'm not going to be calling the endpoint every time I want to paint the titles, i have to save what comes in the json in the sqlite, and then extract from there the texts to put on categories screen ... how can I do this? I'm new, never build a structure like that, I have to do it with sqlite, but I wouldn't know how to start or how to build the tables, if I make a table, only the first tree of the categories will go, and the rest? the children of the nested json? Could someone guide me?
I leave you a piece of the json (for other reasons I can not put it complete) to put them a little more in context
{
"res": 0,
"categorias": [
{
"id": 1,
"name": "Bolsas",
"img": "hogar.png",
"titulo": 0,
"children": [
{
"id": 11,
"name": "Para llevar",
"img": "llevar.png",
"titulo": 0
},
{
"id": 12,
"name": "Para Conservar",
"img": "consrvar.png",
"titulo": 0
},
{
"id": 13,
"name": "Para Organizar",
"img": "organizar.png",
"titulo": 0
},
{
"id": 14,
"name": "Multipack",
"img": "multipack.png",
"titulo": 0
}
]
},
{
"id": 2,
"name": "Cuidado del aire",
"img": "aire.png",
"titulo": 0,
"children": [
{
"id": 21,
"name": "Instantaneo",
"img": "instantaneo.png",
"titulo": 0,
"children": [
{
"id": 211,
"name": "Aerosol",
"img": "llevar.png",
"titulo": 0
},
{
"id": 212,
"name": "Toque",
"img": "consrvar.png",
"titulo": 0
}
]
},

You have several option to work with json and sqlite:
Just put your json as string in row on table and then select as string and convert to json.
You can use similar approach from paragraph 1 bud with using sqlite's json extension. It adds possibility to select of update parts of json by special query syntax. To use sqlite with this extension u will need custom build of sqlite like sqlite-android (with have many other benefits).
You can crate separate sqlite table from each level fo json nesting (depends on your need of granularity). For example you can create table categorias with columsn(categorias_id, categorias_name, categorias_img, categorias_titulo), table categorias_childrens with columns(categorias_id, id, name, img, titulo, categorias_childrens_id).
In this case all rows of categorias_childrens table related with categorias.categorias_id contains categorias.categorias_id in categorias_childrens.categorias_id column. And all rows related with some of categorias_childrens row contains categorias_childrens.categorias_id in categorias_childrens.categorias_childrens_id column (situation when children.id.21 has childrens ).
Or u can combine approaches fro example: create table categorias with columsn(categorias_id, categorias_name, categorias_img, categorias_titulo, children) where column children will be contains string json value.

Related

Query problem using sembast (NoSql) with flutter

I'm very new to NoSql databases, and I just want to ask you an easy question about the use of sembast! I'm developing a very simple app with flutter, and I want to get the object inside the array "list" with the "name" equal to 1.
{
"id": 12345,
"list": [{
"name": 1,
"element": [{
"nameItem": "a"
}, {
"nameItem": "b"
}]
}, {
"name": 2,
"element": []
}, {
"name": 3,
"element": []
}]
}
So I want to make a query that retrieves me this information:
{
"name": 1,
"element": [{
"nameItem": "a"
}, {
"nameItem": "b"
}]
}
I've written this code, but it doesn't work: I don't understand how to make a query with a subtag as a key in the json tree.
Future<List<ElementList>> getElementFromList(int name) async{
final finder = Finder(filter: Filter.equals("name", name));
final recordSnapshot = await _elementList.find(await _db, finder: finder);
return recordSnapshot.map((snapshot){
final elementObj = ElementList.fromJson(snapshot.value);
return elementObj;
}).toList();
}
this returns me []. How can I solve the problem?
Thank you in advance!
Sembast queries allow to filter records, not part of a record. If the object you mention is a whole record, you can:
use custom filter to perform the lookup yourself for each record in the database (checking each item in the list field`)
when a record is retrieved, extract the item (doing a similar Map/List manipulation)
See an issue with a complex filtering

How to access String name and price from this JSON data?

I have established a connection but unable to access data.Help needed
JSON data
[
{
"name": "Margherita",
"price": 170
},
{
"name": "Corn MAnia",
"price": 170
},
{
"name": "Triple Tango",
"price": 210
}
]
In MarkLogic Server, you can use the XPath language to traverse JSON and retrieve data. Code can be written in the Xquery language or Server Side JavaScript:
xquery version "1.0-ml";
let $doc:= xdmp:unquote('{"root":[{"name": "Margherita","price": 170}, {"name": "Corn MAnia","price": 170}, {"name": "Triple Tango","price": 210}]}')
return xdmp:document-insert('/doc/names.json', $doc)
To get data, use:
doc('/doc/names.json')/root[1]/name
or
doc('/doc/names.json')/root[2]/price/data()

How to write and read contents containing complex json data?

friends:
I get a response from remote server as follows on Android:
{
"items": {
"persons": [
{
"id": "200120",
"name": "Bill"
},
{
"id": "200121",
"name": "Jim"
}
],
"tasks": [
{
"id": "001",
"name": "Fetch ten books",
"deadline": "5:30 p.m.",
"scores": "10"
},
{
"id": "002",
"name": "Fetch thirty books",
"deadline": "5:30 p.m.",
"scores": "30"
}
],
"intro": "This is a funny game."
},
"otherObj": []
}
And I want to save it to phones. I do not think it a good choice to save it into databases. However it read not fast if put the response to SharedPreferences file. Is there any other way?
No need to save this data into database as these value are dynamic and will change time to time.
In your Activity, call the API and get JSON as response and then parse this JSON and show it on device.
To store it on device u can use Singleton class also.
For JSON parser refer to http://www.tutorialspoint.com/android/android_json_parser.htm
You can first save your data into objects and at last into Object Array and then Save to database.(If your data is dynamic it is better you only deal with Object not database)
For saving to database you can use
db.beginTransaction();
for(object : ObjectArray){
db.dboperation();
}
db.setTransactionSuccessful();
db.endTransaction();

TouchDB Android dynamic views for query

I successfully got my Android app to sync with my iriscouch database. In this database I have about 100 documents, each representing one line of public transport with its corresponding stations, like:
{
"_id": "1",
"_rev": "1-9658663cc48789829fb505ce2e0fd4ce",
"name": "1",
"stations": [
{
"_id": "Stefan-Fadinger-Platz",
"name": "Stefan-Fadinger-Platz",
"long": "16.35464100",
"lat": "48.16771000"
},
{
"_id": "Julius-Raab-Platz",
"name": "Julius-Raab-Platz",
"long": "16.38323500",
"lat": "48.21135300"
},
{
"_id": "Schwedenplatz U",
"name": "Schwedenplatz U",
"long": "16.37765200",
"lat": "48.21178500"
},
....
}
How can I access this data from Android based on user selection, for example getting all stations for line 1?
As far as I've read, I need to create a view for each query, but I don't think manually creating 100 views is performant. Is there a dynamic way?
You can create views locally in TouchDB.
http://wiki.apache.org/couchdb/HTTP_view_API
They work the same as regular CouchDB views. You can provide parameters to the views that help you filter based on a user selection.

Android GSON: Parsing several different objects from the same response

I'm working on porting an iPhone app that relies heavily on JSON to Android.
One of the responses that has to be parsed is used to build the main screen. The JSON response for this contains 3 different objects, namely Icons, Header and Player. These are all contained within the object Home.
Icons and Player both contain an Array of items, the Header is just a single item.
Now I'm still a beginner when it comes to JSON, and I'm not quite sure how I should parse this response. Therefore I would like to know if I have the right idea before working myself into problems.
My idea is to create 4 different classes, one for Home, icons, Header and Player.
Home would contain an array of both Icons and Player, and an object of Header.
But I'm not sure if this is the correct way to do this.
The JSON response in questions is as followed: (Removed some objects due to the size of the response)
{
"Home": {
"Icon": [
{
"ScreenID": 533,
"ScreenIndex": 1,
"IconName": "mainIcon_news",
"Title": "News",
"FK_ModuleID": 6,
"FormID": 567,
"ModName": "News",
"MediaType": "",
"New_Icon": 0
},
{
"ScreenID": 528,
"ScreenIndex": 2,
"IconName": "mainIcon_music",
"Title": "Music",
"FK_ModuleID": 3,
"FormID": 562,
"ModName": "Media",
"MediaType": "Music",
"New_Icon": 0
}
],
"Header": [
{
"ModHomeRotationID": 183,
"image_url": "*****/Media/68/1216_5.jpg",
"flg_RotationEnabled": false,
"flg_RotateOnlyOnReturn": true,
"flg_RotationRandomize": false,
"flg_RotationDelayMS": 5000,
"flg_RotationDelayFadeMS": 3000,
"HomeRotationIndex": null
}
],
"Player": [
{
"MediaID": 1219,
"Track_Name": "***",
"song_url": "*****/Media/68/1219.mp3",
"song_remote_url": null,
"FileSize": 4700502
},
{
"MediaID": 1220,
"Track_Name": "**** ",
"song_url": "*****/Media/68/1220.mp3",
"song_remote_url": null,
"FileSize": 4350222
}
]
}
}
Could someone tell me if I'm in the right direction, and if not, what I should be doing instead?
I should mention, I'm using GSON to parse the JSON responses at the moment.
Thanks in advance
Yes you are right you need to create Four classes and need to initialize the values inside that class name..
Validate your Json using JSONLint :
Then try this sample Parsing JSON using GSON and One More

Categories

Resources