How to get JSONArray from WebService using Asynctask - android

I want to ask I have some JSON file returning from WebService I can detect some returning from JSON file but if I want to get JSON array from there cannot do that? But if do that manually like I only use JSON returning from flat data without Asynctask and same returning but can detect.
Problem is: If I use returning JSON file from WebService array cannot be detected on there
But if I use flat data and have same return can detected array

Related

How to read object name using JSONReader

I am developing an android application and i need to parse the json data from YQL
https://query.yahooapis.com/v1/public/yql?q=select%20%20*%20from%20%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%27YHOO%27)%20and%20startDate%20%3D%20%272016-01-01%27%20and%20endDate%20%3D%20%272016-04-10%27&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
I am using JsonReader to access the Count and the Result object. The problem am facing is how do I check the object name and then call beginobject on results object . I am able to fetch the counts value though from the Json Feed

Where to format data from a JSON Object

I have to convert data from metric to imperial, depending on the user's preferences.
But I wonder where to convert this data. Should I do it in my AsyncTask that retrieves the JSON Object and parse value to the model or should I simply do it in the UI thread in my fragment when getting the values from the model?
I'm asking this because I will have to adapt my Strings when calling setText() depending on User's preferences(eg: °C or °F, m or Ft).
Just want to know what is best in this case
Thx!
For parsing JSON data you should do the parsing steps in doInBackground of AsyncTask. And after parsing set the parsed data to the fields in onPostExecute. Check this Android JSON parsing tutorial if you are new to JSON parsing.
Do not perform formatting operations on UI thread, as it causes performance issues for you app if data is huge, you can even do text formatting in onPostExecute() method.

Decode json response in android

I need to decode a json response sent from php in android.
JSON Response
{"VehicleNumber":"1234FB14","Make":"BMW","Model":"X5","Type":"Car","Color":"Black","EngineCapacity":"1800cc","Fuel":"Diesel","DateOfReg":"31-Dec-2013","OwnerNIC":"B1234567890123","serviceArray":[[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1800","Description":"tponggshmbch"}],[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1900","Description":"tponggshmbch"}]]}
The response consists of values for their respective keys and I can retrieve these value by just calling getString(key) in java. {key:value,2D Array[][]}
The problem is that I can't decode the 2D Array being passed here. I get it in a string format in java and if I display it in my android app I can view this:
[[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1800","Description":"tponggshmbch"}],[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1900","Description":"tponggshmbch"}]]
But i need to retrieve all the value from the 2D array.
Any help please.
If Your json is quite big I recommend using gson or Jackson libraries.

Parsing JSON through Gson method fromJson

I am parsing an API through
ArrayList<Spot> spots = Gson.fromJson(response , new TypeToken<Arraylist<Spot>>(){}.getType())
My response is perfectly fine, but when I see the response through Gson.toJson(spots). It shows me empty objects, means it have parsed the first field of each object in the API but internals fields of each object is not parsed.
I receive null values if I access the internal fields which is obvious . Can any one let me know where the problem lies, why its not parsing any internal field? .
Maybe you have to use List instead of ArrayList.
When you create your json use:
List<Spot> spots = new ArrayList<Spot>();
gson...
And when you parse the response use:
List<Spot> spots = Gson.fromJson(response , new TypeToken<List<Spot>>(){}.getType());
If that does not work, please show your json and the Spot class.

json parsing in android of web data

I have to parse this json data. The data begins with [ and ends with ]
How can we parse such json data? json data usually starts with {..[..]..}
Just create a JSONArray from your input. There is even a constructor taking a String as parameter. So, basicly you need to do something like this:
String input = .. //read your input
JSONArray arr = new JSONArray(input);
//work with the array as usual..
take result data as a JSONArray and not a JSONObject.
It depends on how you parse the data. Built in json or google json(gson) etc.
But normally you dont have to care about that it starts with square bracket.
Show me what the json array/object look like and I can give you an example.

Categories

Resources