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.
Related
I have a data class (generated using JSON to Kotlin Data Class plugin) below that is used to parse json data from an object detection server. The server returns the "images" in multidimensional array. I am wondering how can I convert this JSON data in format that can be displayed in my ImageView in Kotlin.
Data Class
JSON Response
Your answers will be highly appreciated. Thankss!
Currently, I have this code that transform the images array into a bitmap. However, no image was being displayed in my ImageView.
Current Code
I'm using com.fasterxml.jackson.databind with Retrofit to handle the response from the server in my Android app.
Since the JSONObject response is too complicated and contains lots of JSONArray, I want to be able to parse some of those array fields into String instead of creating POJO for each sub-object that those Array could contains.
Is there a way I could just tell Jackson to keep those field as String and not parse them into Entities?
I am new to Android and I got an assignment where I have to use JSON parser to parse the link and display the content in tabular form and its table field. When clicked, it should open some sections. Please, if anyone knows any kind of tutorial on how to do it, it would be really great. Thank you.
You can use gson to parse the incoming data into POJO. It will be as simple as writing a class that matches your JSON data format.
or just just the GSON framework from google
you will need to create a Pojo(or Bean) with basic constructor and fileds matching the name of the json String.
something like:
Gson gson = new Gson();
gson.fromJson(jsonProvider, new TypeToken<Contact>(){
}.getType()
);
in this case the POJO is named Contact. After that u have an object filled with ur json data that u can use to do what ever u want...
hope this helps
I am sending json object to server, My url having json object.Json object having special characters,how to encoding special characters in android, please help me.Thanks in advance.
Use UrlEncode.encode(yourstring, "UTF-8");
If I understood you, you want to send a json object using the url itself (that is, as a parameter). If security doesn't matter you (it will be visible), you could just encode using [Base64][1].
You should probably play with your json object to convert it to a byte[], called jsonbytes, for instance, then use Base64.encodeToString(jsonbytes, Base64.URL_SAFE) and sending this as a parameter.
Your server then should convert this Base64 encoded string into a json object, which tends to be straightforward if you're using PHP:
$jsonString = base64_decode($_GET['json']);
$json = json_decode($jsonString, TRUE);
This will give you an associative array in PHP. If you just want the json string, skip the last step.
I have an XML web service. I want to parse this XML and I want to store in an separate textviews. The following is an XML content, and I have finished getting it in a String variable.
{
"30_year_rate": 4.25,
"30_year_pi": 196.78,
"30_year_apr": 4.375,
"20_year_rate": 4.375,
"20_year_pi": 250.37,
"20_year_apr": 4.5,
"15_year_rate": 3.75,
"15_year_pi": 290.89,
"15_year_apr": 3.875,
"10_year_rate": 3.625,
"10_year_pi": 397.89,
"10_year_apr": 3.75,
"5_year_arm": 2.75,
"5_year_pi": 163.3,
"5_year_apr": 2.875,
"7_year_arm": 3,
"7_year_pi": 168.64,
"7_year_apr": 3.125,
"3_year_arm": 4,
"3_year_pi": 190.97,
"3_year_apr": 4.125,
"5_year_io_arm": "N/A",
"5_year_io_pi": "N/A",
"5_year_io_apr": "N/A",
"QuoteID": 1449,
"Licensed": "N"
}
How can I parse this data? I want to convert it to a JSON object and retrieve it, or any other reasonable approach.
If what you're getting back from the webservice is the string above, then you already have a JSON string. To create an object that can retrieve information from it, use something like JSONObject.
JSONObject object = new JSONObject(your_string_variable);
double thirtyYearRate = object.getDouble("30_year_rate");
String licensed = object.getString("Licensed");
etc.
You might (will) run into some issues where you try to pull a double from a JSON field that contains a string; i.e., the "N/A" fields above. You'll likely have to pull them out as strings and then try to parse doubles from them, and if the parsing throws an exception, you'll know it's a string.
Alternately, you could look into JSON binding with something like Jackson, which apparently runs on Android.
To parse JSON, you could use the built in JSONObject org.json Or Json-lib if you use an old version of android.
To parse XML, use XMLPullParser. A sample can be found here: Parsing XML Data