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
Related
I have always used a fromJson method to convert my json object to my model object. So let's say that my JSON has a car field which has my car models data, so I always parse the Json. Now I found out that it's also a common practice to use the json objects directly in the application. That has gotten me thinking
should I parse the json back to my models, if yes then why and if no then why not?
It depends but in general - yes, you should have transformation logic to convert your jsons (essentially, DTOs) into your entities (models as you call them). Here is my reasoning:
Your entities will likely be different from corresponding DTOs. As an example, your json model can have date/time information as long UTC msecs, but your entity model will likely be more usable if it has localized Date's
If you're exposing your entities (for example, as part of a library), you'll have much more flexibility in making changes to your remote apis (if such exist of course) without breaking your library's consumers codes
It completely depends on your project structure, if you have POJO classes defined in the models then converting them is the way to go. It makes your code more readable and fulfils your model which makes fetching the data easier. If you use a JSONObject directly then pulling data from it requires a bit more code. In terms of performance they both should have equal impacts since both are 'Objects' and will consume resources equally.
I vote for parsing them back to Models because of the simplicity it
enables for using those values further in your coding.
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.
I'm working on a little Android project in java, I'm using the webapi of spotify to get me so data to play around with.
First thing I notice when visiting: https://developer.spotify.com/technologies/web-api/ is that a developer can choose to use xml or json.
What i would like to do is 'cache' my data so i can do the following:
read one value from the file, or construct an object based on different values
add the object created above to the lisviewadapter
update the listview with the updated adapter
iterate over previous steps until parsing of file is complete
I want to be as quick as possible for my users so that the ui gets updated while parsing and loading the data. For this I sketched 2 use-cases.
add items to the listview until done
show a X amount of items and show a load more button
What file type is more ideal to use for my problem? xml parsing or json parsing?
How can i do this parsing? which tools meet my requirements?
Which use-case seems better to use?
any tips if you have done something like this in the past?
any guidance is appreciated
I don't make any distinction between XML or JSON. To cache data you have two options. Either write the data to a file (will require reparsing of the file to extract the data (unless you create a new file of, for example, CSV data)) or write the required data to a database after parsing. As for updating ListView, that would be up to you based on the number of entries you have.
Android has both XML and JSON parsers built-in. You don't need GSON.
I'd recommend JSON because JSON uses less bandwitch for transfering and google has made an awesome json parser library called GSON.
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.
I am new to Android. I need to store arraylist or vector in a database(SQLite) as a value for one of the column. I have searched regarding storing arraylist or vector. In that they suggested to convert arraylist into JSON.
Is there any other solution for this problem other than converting arraylist into JSON. Please give a me a solution or article regarding this issue.
Vectors and lists are data structures. SQLite columns only accept atomic values, namely numbers, strings and blobs.
Therefore you need to serialize (convert) the vector/list into a single string or blob - numbers won't do anyway. You will have to use an encoding that is uniquely reversible and will preserve any special properties of your data structure.
Sure, you can re-invent the wheel and use your own encoding. Or you could semi-reinvent the wheel by using the Java Serialization API. Or you could use something like JSON, which is a more tried solution.
If I were you, however, I'd first consider changing the database schema. Lists and vectors are close enough to the relational model that you might be able to store them in the database without serialization techniques.
EDIT:
See also this question and this one.
Firstly if you have a ArrayList or Vector of Strings . Then simply traverse it and store each of its string and manage it by your application logic.
If it is not carrying Strings then simply serialize the ArrayList object over any file.
and keep the filename in the database
how can you store a memory object to db? :) you should serialize it to string. no matter what you will use. xml, json, your own toString method. just choose what is easier for you to serialize and deserialize objects. json seems to be a good idea
Depending on your application and the use cases for it I would create a separate table in the database for your ArrayList data (with one-to-many or many-to-many relations), and query for them separately.
Yes, it would most definitely add some complexity to your application but it would be a more robust and fault tolerant solution (not to speak of more elegant ;-)
This is however hard to rectify for a very simple use case where duplicate ArrayList items don't occur...