I need to import data from a file to my application. The obvious choices are XML and JSON. I have heard that JSON is lightweight and when parsed with Jackson, it gives good performance. But I have also heard that JiBX for XML is fast and uses XMLpull to give good performance. I would like to know which option to go for and why?. Can I get a speed comparison of XML with JiBX and JSON with Jackson? Also, I want to know if Google Gson is better than Jackson for JSON parsing.
Json is the light weight.If you want to use large documents use, JSon with Jackson.
Excellent explanation been given in this article(especially read Note:). Xml you have
different types like DOM,PULL and SAX.But as per my knowledge, JSON is the best. For Large
documents,prefer Jackson. http://www.javabeat.net/2011/05/androids-json-parser/
For Jackson and gson. Have a glance of this link.
Jackson Vs. Gson
So when comparing with xml and json,i always suggest you to use json, since it's a light weight data for android. So it will be fast in loading and display the data. And gson,
it depends based on your project. Please see the above link comparsion.you will cleanly understand.
In addition, Jackson can also do XML if that's what you need, with https://github.com/FasterXML/jackson-dataformat-xml
I also agree in that Jackson+JSON will be faster than any of XML-based solutions (as per https://github.com/eishay/jvm-serializers). JibX is not bad and probably is fast enough for most uses (as do many, many other options). But if speed is your thing, Jackson will be faster of choices you mention.
I agree that in pure performance, JSON is going to be faster than JiBX.
You choice of tools should depend on the data you are transferring.
Use JiBX if you have a concrete data definition. JiBX is especially good at creating and retrieving complex data objects. JiBX will make sure your data is correct and automatically convert your data to and from Java objects.
Use JSON if you want more flexibility. JSON doesn't check to see if your data is correct.
For example, when you create an object in JiBX:
Person person = new Person();
person.name = "Don";
person.age = 52;
When you retrieve the information in JiBX:
System.out.println("Name: " + person.name);
System.out.println("Age: " + person.age);
In JSON, your code would look like this:
JSONObject person = new JSONObject();
person.put("name", "Don");
person.put("age", new Integer(52));
To retrieve the transmitted information:
String name = person.get("name");
long age = person.get("age");
As you can see, the JiBX code is nicer to read, but the JSON is more flexible since you don't have a schema definition.
In either solution, your code is exactly the same in your Android client and your java service/server.
I hope this helps.
Don Corley - JiBX contributor
Related
I have thousand of strings in Json and I wanted to convert into xml(strings.xml) .Is there any short way to do that ? plugins ? or I have to do it one by one?
If you want to convert the json to xml in your app, you can use any existing library. This answer refers to one such.
If you just want to simply convert a json contents to xml, you can use any online converter. This one came first in my google search result. (But I would check the site's reliability before uploading any sensitive data)
You can read the entire JSON into a String object, and get the XML representation with Regex and .replace() method Of a String
Here is a smaple
val json = """{
"text_download_complete":"your file is downloaded"
}"""
val xml = json.replace("\\{\\s*".toRegex(), "<string name=")
.replace(":\\s*\"".toRegex(), ">")
.replace("\"\\s*\\}".toRegex(), "</string>")
Log.d("LOG_TAG", "onCreate: XML:\n$xml")
You can also read the JSON file instead of coping the entire file into a String...
I believe that won't be the optimum solution, but I think it would be a one shot task, and just wanted to be simple rather than looking at performance.
I am getting data from an api which cannot be converted into pojo so am not able to get the data in a normal manner
Data that i am getting
{"TABLE_DATA":"
{\"data\":[
[\"Tiger Nixon\",\"System
Architect\",\"Edinburgh\",\"5421\",\"2011/04/25\",\"$320,800\"],
[\"Garrett
Winters\",\"Accountant\",\"Tokyo\",\"8422\",\"2011/07/25\",\"$170,750\"],
[\"Ashton Cox\",\"Junior Technical
Author\",\"SanFrancisco\",\"1562\",\"2009/01/12\",\"$86,000\"],
[\"Cedric Kelly\",\"Senior Javascript
Developer\",\"Edinburgh\",\"6224\",\"2012/03/29\",\"$433,060\"],
[\"Airi
Satou\",\"Accountant\",\"Tokyo\",\"5407\",\"2008/11/28\",\"$162,700\"],
[\"Brielle Williamson\",\"Integration Specialist\",\"New
York\",\"4804\",\"2012/12/02\",\"$372,000\"],
[\"Herrod Chandler\",\"Sales Assistant\",\"San
Francisco\",\"9608\",\"2012/08/06\",\"$137,500\"],
[\"Rhona Davidson\",\"Integration
Specialist\",\"Tokyo\",\"6200\",\"2010/10/14\",\"$327,900\"],
[\"Colleen Hurst\",\"Javascript Developer\",\"San
Francisco\",\"2360\",\"2009/09/15\",\"$205,500\"],
[\"Sonya Frost\",\"Software
Engineer\",\"Edinburgh\",\"1667\",\"2008/12/13\",\"$103,600\"],
[\"Jena Gaines\",\"Office
Manager\",\"London\",\"3814\",\"2008/12/19\",\"$90,560\"]`
there is no pojo available This is my first time working in an API any guide will be helpful. I am receiving the following data using retofit and rxjava2.
Data format is post method .
This is a jquery format. You may try to use this https://javacodegeeks.com/2013/02/jquery-datatables-and-java-integration.html
Look also this topic stackoverflow.com/questions/48942253/how-to-implement-jquery-datatable-in-android-any-library
The service you get data from may have an opportunity to set data format, you must specify it in your GET query, look in the API documentation.
That looks a lot like a .csv file. CSV files were the old format of Comma Separated Values, built from Tabular data.
Each row is one object and each column is its attribute/field. You can read about CSV files here.
Approach 1
Filter out using string manipulation the extra symbols of slashes and quotes. Delimit at the square braces (more info here) and then reading each delimited comma separated value text by any CSV reading library. Here's a tutorial using Apache Commons CSV library to read CSV files in Java.
Approach 2
Instead of using any library, if your response set is small and you don't need as much parsing optimization, write your object class's constructor to take in each row, filter out the useless symbols (using string manipulation) and initialize your object's fields.
All steps involved:
Clean response and remove unnecessary symbols using string manipulation.
Build a POJO class to keep one row of the dataset. Have its constructor to take in the first row of your dataset to initialize its attributes.
Build a list format or ArrayList format of your previous class with additional methods to sort, search or call by indices as required.
Build a constructor for this list object class to read in your cleaned string response and iterate over it to build Employee objects and adding them to your list.
I have this kind of data. This can be or don't be an array. Just for easy reference.
ArrayName = Array1, Array2, Array3
Array1 = abc, cde, fgh
Array2 = abc, cde
Array3 = abc, cde, fgh, ijk, lmn
So, what are the best method to store this kind of data.
If I want to
Add or delete Array1 and all things inside
Add or delete item in Array2(eg. adding fgh or remove cde)
Methods I discovered:
SharedPreference Android Shared preferences example
Arrays
SQLite Android SQLite Example
Text file
Please share the pro and cons of why you choose the method.
Please also share if there are better ways to store this kind of data.
Kindly edit this if you found a better link or sample for other to reference.
Here are pros and cons for each solutions:
1) SharedPreference
You save simple key-value pairs here. So it is very hard to save array and complex structures in SharedPreference. So the solution will not work with arrays and arrays of arrays. It will be extremely(but not impossible) difficult to achieve what you want.
2) Arrays
Absolutely not! It is memory storage, so when you close app, or on process death, you will lose all data
3) SqlLite
I would add to this other Databases for android, like Realm.
Good solution. It is structured storage for collection of data. It will be very easy for storing/retrieving data when it is structured as rows. Furthermore you can delete rows easily. You don't have to read whole structure (other arrays) when you need particular row, or particular array (table in this case)
4) TextFile
I don't recommend to store in a text file, but it is possible to do so, you can serialize those arrays to text file, and deserialize. But every time you have to do this, and to read whole structure and parse it even if you want only e.g. Array2. It can be slow when your data becomes bigger.
It's incredibly hard to give advice with such vague requirements, you apparently have data structured as an array of arrays of strings, and you want to store it persistently on Android - and that's basically all we know.
In addition to the solutions mentioned, I would consider using GSON to store this as JSON to disk. While read/write may not have optimal performance, it's very easy to model documents with things like arrays of arrays, and we have no way of knowing your performance requirements vs ease of use.
class MyData {
public List<List<String>> data;
}
If you then have a MyData object, you could simply serialize it to a string, which could be written to a file on disk:
String json = new Gson().toJson(myData);
This would produce something like
{
"data": [
["abc", "cde", "fgh"],
["abc", "cde"],
["abc", "cde", "fgh", "ijk", "lmn"]
]
}
which could easily be written to disk using e.g. standard File and BufferedWriter. You can then read it back and deserialize using:
MyData myData = new Gson().fromJson(json, MyData.class);
Android JSON parsing is rather straightforward until it comes to have json reserved characters in your keys/values. I have JSON coming from an HTTP socket whose response is put into a string variable. It looks like this
{"ZboAdtPw4bA":"Ben Heck"s PlayStation 4 Slim Teardown","iC4qIx72_Cc":"Ben Heck's Xbox Slim Teardown"}
See the double quotation in the first value? It even screws up on StackOverflows web page. How am I supposed to escape/prevent this from happening? If I do a:
response = response.replace("\"", "");
Then all the double quotation get replaced, not just the ones in the key/value pair. This is because its all contained in one string at the moment. I am wondering if there is an easy way to do this with android. And of course, java answers are acceptable to. Now I could do this since its just a single dimensional key/value pair easily, I may not even need JSON, but I would like to adhere to standards.
you are simply trying to ruin the basic of a JSON .
you simply add
"/"" to the java code .
other than that its not possible for the parser to differentiate between the quotations from the JSON format or the quotations in the string .
how to extract some criteria from this page http://www.zigwheels.com/api/zigtvApi.php?method=data&module=News§ion=News
and filter this ( content_id , thumbnail, summary , headline , image) to display them as rss feeds in my android GUI
The output from that URL looks like a JSON feed. You can easily parse JSON data in Android using the JsonObject - see this tutorial for a comprehensive example.
A better (and probably easier) solution would be to use Google Gson and extract the object that way. I've written a full (compilable) program you can use as an example here.
You are receiving a JSONArray from that page. You should create some objects from that JSONArray which will be later displayed in a ListView. For retrieving objects from that feed you could use Json or Gson as #Marvin Pinto suggested, or you could also have a look at my ObjectFactory project, which is a very simple and easy to use parser. It can simply create your objects from a json or xml feed, and you could also use it asynchronously.
After you fetch your objects, with your desired option, you can create a ListAdapter and use it to display your objects in your ListView.