I am using PHP as a middleman to access a MySql database and it returns the result of the query as a json string using json_encode, then display it within the TableLayout of the app, this is why order is important so I can line up the data and the headers.
After some research I found out that json does not enforce order so any time I call new JSONArray(result) it scrambles the json returned by PHP. Is there any way to preserve the order of the returned string? Or maybe I'm using the incorrect data structure on either end.
Relevant PHP result:
[{"FIELD1":"vsa","FIELD2":"dfs","FIELD3":"dsfa","FIELD4":"adsf","FIELD5":"23","ZIPCODE":"asdf","USERNAME":"asd","PASSWORD":"as","DATE1":"dsfa"}]
Relevant Android Result After JSONArray(result):
[{"ZIPCODE":"asdf","DATE1":"dsfa","FIELD3":"dsfa","FIELD2":"dfs","FIELD5":"23","FIELD4":"adsf","USERNAME":"asd","FIELD1":"vsa","PASSWORD":"as"}]
I believe the reordering inside a JSON object is due to the fact that JSON objects are key/value pairs (not an indexed array), which by default are unordered. However, the JSON array is an ordered sequence of values (JSON objects).
Don't rely on order!
Source: http://www.ietf.org/rfc/rfc4627.txt
I've never seen new JSONArray(String) change the order of anything, and I've used it a lot. However, what you have seems to be an array of length 1. Using myJsonArray.getJsonObject(0).getString("ZIPCODE") should still return the correct data, and as long as you query in the correct order (FIELD1, FIELD2, FIELD3, etc), you should be fine.
Related
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.
E.g. I have next data in my Firebase Realtime Database (my json file contains json array of some objects):
I would like to get all objects starting from a specific index (position) to the end of this array (in Firebase DB json array is converted to simple object which contains children with keys 001, 002 and so on)
So I need something like this but for Firebase query:
list.subList(10, list.size)
I know there are limitToFirst, limitToLast methods but it's different
fireDatabaseReference.child("episodes").limitToLast(10)
It won't do what I need. Because I need to know the size of this array and I need to be sure that this array won't become bigger at the moment someone makes such request (at some point this array can get become bigger due to adding new objects)
Would be great to have a method like from to get all children from 10 to the end (so first 9 children are excluded):
fireDatabaseReference.child("episodes").from(10)
But there is no such method
Is there any solution for this?
Solved :)
fireDatabaseReference.child("episodes").orderByKey().startAt("10")
I want to parse JSONObject and get its keys in the order they do represent to me when i receive. I don't want use JSONObject.keys() as this function giving undefined order of json keys and fully unordered,and this is a huge problem right now because I'm stuck in this position and i have to get json object keys.So is there any way to achieve it? Doing researches i never came across to code which can parse json object keys except JSONObject.keys() which giving reverse order.
There is no way to achieve what you want with a JSONObject because JSONObjects are not ordered by definition and you shouldn't rely on the insertion order. If you need something ordered you should look into JSONArray
I have noticed that although JSONArray gives the proper order, the order won't be kept in HashMap, if you're using it for saving the values.
Using LinkedHashMap solves the order issue.
Im trying to develop an application that accesses a database through POST requests and returns values in the database.
Im curious about something tho. what if i wanted to pass a number or a list to the database, such as the list of columns to grab for each row. Is it possible to extend the BasicNameValuePair class to acomplish this, or am i missing the correct way to do this?
Extending the BasicNameValuePair is not going to help you as the parameters of a HTTP request need to be name value pairs. If you want to pass more parameters, add more name value pairs and handle them in the server.
If the number of parameters are high, it would be prudent to use HTTP POST. If the parameters are going to be complex, it would beneficial to submit the request in a more user-friendly format like JSON or XML and parse it on the server side. However, if it is just the name of the columns of the table row, you can also think of using a format like comma separated list.
http://mydomain/getdata?id=10&columns=1,2,3,4
In case of the above, you need to parse the columns from the comma separated list at the server side, whereas you are creating name-value pair for columns as follows:
BasicNameValuePair columnData = new BasicNameValuePair("columns", "1,2,3,4");
Hi, I am new to android. I want to get data from webservice. in which key's are store in an array . and ley values stores in another array. In webservice data are display like,
{"1":{"para_id":"1","para_parent_id":"0","para_level":"0","para_type":"0","para_value":"Salution","para_desc":"Salution","para_sort_order":"1","para_tech_desc":"Salution","created_dt":"2011-08-11 19:47:05","created_by":"1","updated_dt":"2011-08-11 19:47:05","updated_by":"1","status":"A"},"1001":{"para_id":"1001","para_parent_id":"1","para_level":"0","para_type":"1","para_value":"Mr.","para_desc":"Mr.","para_sort_order":"1","para_tech_desc":"Mr.","created_dt":"2011-08-11 19:47:21","created_by":"1","updated_dt":"2011-08-11 19:47:21","updated_by":"1","status":"A"},"1002":{"para_id":"1002","para_parent_id":"1","para_level":"0","para_type":"1","para_value":"Ms.","para_desc":"Ms.","para_sort_order":"1","para_tech_desc":"Ms.","created_dt":"2011-08-11 19:49:00","created_by":"1","updated_dt":"2011-08-11 19:49:00","updated_by":"1","status":"A"},"1003":{"para_id":"1003","para_parent_id":"1","para_level":"0","para_type":"1","para_value":"Mrs.","para_desc":"Mrs.","para_sort_order":"1","para_tech_desc":"Mrs.","created_dt":"2011-08-12 09:40:14","created_by":"1","updated_dt":"2011-08-12 09:40:14","updated_by":"1","status":"A"},"2":{"para_id":"2","para_parent_id":"0","para_level":"0","para_type":"0","para_value":"Appointment Types","para_desc":"Appointment Types","para_sort_order":"1","para_tech_desc":"Appointment Types","created_dt":"2011-08-16 15:43:15","created_by":"1","updated_dt":"2011-08-16 15:43:15","updated_by":"1","status":"A"},"2001":{"para_id":"2001","para_parent_id":"2","para_level":"0","para_type":"2","para_value":"Scheduled","para_desc":"Scheduled","para_sort_order":"1","para_tech_desc":"Scheduled","created_dt":"2011-08-16 15:43:36","created_by":"1","updated_dt":"2011-08-16 15:43:36","updated_by":"1","status":"A"},"2002":{"para_id":"2002","para_parent_id":"2","para_level":"0","para_type":"2","para_value":"Cancelled","para_desc":"Cancelled","para_sort_order":"1","para_tech_desc":"Cancelled","created_dt":"2011-08-16 15:43:51","created_by":"1","updated_dt":"2011-08-16 15:43:51","updated_by":"1","status":"A"},"2003":{"para_id":"2003","para_parent_id":"2","para_level":"0","para_type":"2","para_value":"Re-Scheduled","para_desc":"Re-Scheduled","para_sort_order":"1","para_tech_desc":"Re-Scheduled","created_dt":"2011-08-16 15:44:24","created_by":"1","updated_dt":"2011-08-16 15:44:24","updated_by":"1","status":"A"},"2004":{"para_id":"2004","para_parent_id":"2","para_level":"0","para_type":"2","para_value":"Deleted","para_desc":"Deleted","para_sort_order":"1","para_tech_desc":"Deleted","created_dt":"2011-08-16 15:44:38","created_by":"1","updated_dt":"2011-08-16 15:44:38","updated_by":"1","status":"A"},"3":{"para_id":"3","para_parent_id":"0","para_level":"0","para_type":"0","para_value":"Customer Status","para_desc":"Customer Status","para_sort_order":"1","para_tech_desc":"Customer Status","created_dt":"2011-08-29 12:51:48","created_by":"1","updated_dt":"2011-08-29 12:51:48","updated_by":"1","status":"A"},"3001":
I want to sore key "1" ,"2", "3" in an array and related value in another array.and get the data. How can i do this?
Well, this looks like JSON, so use a JSON-Parser, for a tutorial see here. You can make your JSON more readable (especially when asking on SO) using the JSON beautifier