How to parse strange json - android

I have a project which requires the json objects to be saved in database and then based on the users action will display info.
I can get the json from the API using here:http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/ but need help with parsing the following which is a part of json objects.

It really depends on your data structure and amount of entries. If amount is small and queries / sorting requirements are simple, you may just bypass database entirely and parse JSON with buil it (not so good solution in terman of memory consumption) or pull parser ( like GSON - small external dependency bu way better perfromance )
Another option would be just store JSON documents in database, and some fields from them as separate columns for querying and indexing.
If you like to have complex object hierarchy you will find that you are reimplementing
some ORM solution.

Related

Is there something similar to OPENJSON in android?

OPENJSON is a table-valued function that parses JSON text and returns objects and properties from the JSON input as rows and columns.
OPENJSON
I want to deserilize Json into objects, but I want to access parts of the Json not parse it all.
I'm using android Kotlin with Room database, if there is a solution in Room maybe.
You can use any pure java library in android. Google GSON would be a good choice and with some minimal tweaking you can ignore unnecessary parts of JSON objects

what is the purpose of using JSON in android?

I need to know the purpose of using JSON in android ?
Please anyone tell me in a simple way...
Thanks
The same reason you'd use it on any platform. JSON is a way of storing and expressing information. It uses attribute-value pairs in a hierarchical structure. In Android specifically, you may need to download some information from a database, which could be stored in JSON and then read by your app. Alternatively, you could store data locally in JSON but there are probably better and more efficient ways to do that if you're not sending data across a network.
https://en.wikipedia.org/wiki/JSON
JSON is very light weight, structured, easy to parse and much human readable. JSON is best alternative to XML when your android app needs to interchange data with your server
For example, you can get data Json if you work with database. Or if you work with some API's then you can get data in format Json.
For example an app could fetch data from a server. When using JSON to get the data, the traffic is quite small and the app can easily work with it.
For example you have a server with a database with recipes, and your app displays recipes, the app could ask the server for recipes, and it gets a JSON in return. for example:
{
name: 'Cookies'
ingredients: { 'Butter', 'Eggs', ... /* I don't know, I'm not a chef :D */
...
}
The app can then just read the properties and display them in a neat list ;)
JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is also a subset of JavaScript's Object Notation (the way objects are built in JavaScript
Pls go through this link: http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/
JSON stands for JavaScript Object Notation
JSON is lightweight text-data interchange format
JSON is language independent *
JSON is "self-describing" and easy to understand
* JSON uses JavaScript syntax for describing data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries exists for many different programming languages.
Using JSON in Android is not different than using it on any other platform. The main advantage of the format (in comparison to XML for example) is the small size of the data. This is very important for mobile devices due to the scarce resource those application use - i.e. your mobile app should be able to run with little memory usage, slow internet connection and so on.
Besides Android's framework has built-in tools for parsing / creating JSON objects. Thus it is both easy and efficient to use JSON rather than XML. If you have any project specific reason to prefer another data presentation format - don't worry. It is perfectly fine NOT to use JSON as long as some other format is more suitable for your project.
In short JSON is usually the right choice due to its small footprint and easy of use.

RoboSpice and Jsoup

I have an application where I use Jsoup to get HTML file from the internet and parse it into POJOs. I use a custom Content Provider then to persist my POJOs into an SQLite database. It's a lot of code, and certain things are tricky to implement, caching especially (i.e. how to determine that my object is already in database, how to manage expiration, etc.). From looking over the internet I understood that RoboSpice might come to the rescue, since in handles caching transparently. However, I haven't found any example on how to plug in custom parser (my results are neither JSON nor XML, just pure HTML which I'm parsing with Jsoup currently). I'd therefore appreciate if you could point me to some related example.
Here's a more detailed description of what I'm doing. My app reads certain website to get the lists of certain entries. Those entries are calendar-based, and I'm requesting them month by month. Every month's request returns me a list of entries from that month. I want to make those requests cacheable and queryable, therefore I need a database backend, so that I can run custom SQL queries against it. Which RoboSpice configuration should I use, which extensions, and which code samples could I refer to?
Thanks in advance.
It looks like a good idea to use RoboSpice here, but the way you want to use is a bit out of its natural scope.
Usually people annotation a Pojo, let's say for Jackson, and they request a webservice, then the result is parsed via jackson and you get your Pojo. RoboSpice will simply reformat your pojo into json using jackson as parsing / formatting is a considered a bijection.
In your case, you will have to call your own ObjectPersister for your Pojo class and take care of its persistence format yourself. As you store your pojos into a database, the RoboSpice ormlite module may help but it is still experimental.
Have a look at the sample of the ormlite module of RoboSpice.

Android data storage, When to use SqlLite and when to use JSON, Linq alternatives

I have a few years of exp with webdev using .Net and C#... and one feature I really enjoy there is the linq-expressions for quering for data.. is there anything that simulair for Android using to query against JSONs or against the SqlLite database? (I guess im looking for a typed expression/query-framework).
And for my next question.. Which one of the JSON approach and the SQLLite approach am I supposed to use.. and when? I know this has been asked a million times.. but there doesnt seem to be any strict answers..
Is SQLLite for more complex querying and when Im having a bigger ammount of data, while JSON is used while having a smaller ammount of data and not very much querying is needed?
Could I store all my JSON data in the memmory for faster read/write access?..since I have heard that storing it in the storage/on disk might be a slow process.
But on the other hand I also have heard that SQLLite is slow in generall...
And finally.. when we are speaking of "slow" is it slow like, a single read/write looks up the entire application for a few milisecs or is it slow like compared to a in-memory-database that executes queries and stuff fast as lightning
When to use SQLite
SQLite is one form of how to persist your data, how to secure your data. Generally if you want to store larger quantum of data and have quick access to them(and also if these data are "sensitive"), SQLite is great choice. Hence i disagree with your opition that SQLite is slow, definitely not i guess.
I have SQLite database with approximately with 650 000 records and still smart performance(an usage of indexes is sometimes necessary).
When to use JSON
JSON is lightweight data structure designed for human-readable data interchange and what is main is language-independent. It's very good choice(maybe the best) if you want send data via network(send data to remote server etc.) but i think not very good for data persisting also it's not safe as SQLite is.
Generally you should compare JSON with XML but you can't compare SQLite and JSON which are two different things.
General Idea
SqlLite : For Local Storage
JSON : For Server SideStore
It doesn't make sense to compare SQLite and JSON. One is a storage solution, the other one is a data format.
You should probably read the official guide before deciding which storage solution to choose (SQLite, preferences, file system,...): http://developer.android.com/guide/topics/data/data-storage.html
If you want to go for SQLite persistence you can use some third-party ORM to make queries easier (e.g ORMLite is the most popular one)

Caching downloaded JSON data to SQLite database - is it a good idea?

In my app I have to download JSON data from numerous web services. The data classes I use are fairly complex ones (lots of properties, quite deep inheritance tree, etc.).
I intend to do caching, using a single db table, where I'd store the downloaded JSON data in a VARCHAR column (along with other meta-data containing columns). JSON serialization is being done with the Gson library.
It seems quite convenient to just dump the instances into JSON, and parse them again later when I need them. No need to create custom tables for every class, or write loads of custom serialization code. Also, I can do queries on the cache table this way.
The question: Is this approach an anti-pattern by any means?
There is absolutely nothing wrong with this approach; however, I am going to recommend that you instead use the built in caching storage. See the section called "Saving cache files" in Data Storage for more details.This way you don't hog any precious space if your JSON objects are large in the event of a low memory situation.

Categories

Resources