i am converting a xml content into JSON content and the content is as follows
{
"response":
{
"seatlist":
{
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
},
"userid":"8970ca285d9c4e4d",
"seatnum":12,
"session":"online"
}
}
I am able to get the userid and seatnum in the following way
JSONObject response = json.getJSONObject("response");
out.setUserid(response.getString("userid"));
out.setBalance(Double.valueOf(response.getString("balance")));
Now the problem is i need to parse the following content and need to get "num" value
"seatlist":
{
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
}
here is my code i am using
JSONObject objList = response.getJSONObject("seatlist");
String n = String.valueOf(objList.getJSONObject("seat"));
if(objList.getJSONObject("seat").get("userId").equals(userId))
{
String num = String.valueOf(objList.getJSONObject("seat").get("num"));
out.setSeatNum(Integer.valueOf(num));
}
if i have one seat value i am able to get "num" value else i am getting JSON exception
Pls give me a suggestion in this.....
You want to get a JSONArray in an JSONObject. That is not possible.
Get the Array and then iterate through the array to get the objects:
JSONObject objList = response.getJSONArray("seatlist");
for(int i = 0, i<objList.lenght(); i++){
JSONObject json = objList.get(i);
}
{
"response":
{
"seatlist":
{
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
},
"userid":"8970ca285d9c4e4d",
"seatnum":12,
"session":"online"
}
}
means an object with an object called response. this contains an object called seatlist. this contains 2 objects called seat (but this is wrong! this should be an array!). and so on..
to read this you can use
JSONObject json = new JSONObject(yourresponse);
JSONObject response = json.getJSONObject("response");
JSONObject seatlist = response.getJSONObject("seatlist");
JSONObject userid = response.getJSONObject("userid");
JSONObject seatnum = response.getJSONObject("seatnum");
JSONObject session = response.getJSONObject("session");
now seatlist contains.
{
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
}
which is wrong, since it contains 2 elements with same name. Now you can either call it by index like this:
JSONObject seat1 = seatlist.getJSONObject(1);
JSONObject seat2 = seatlist.getJSONObject(2);
seat1.getString("balanace"); seat1.getInt("num");
or you can iterate through the JSONObject.
At least it should be an Array instead of JSONOBject.
This means it should look like this.
{
"response": {
"seatlist": [
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
],
"userid":"8970ca285d9c4e4d",
"seatnum":12,
"session":"online"
}
}
Related
I'm trying to get this JSONArray with android but I can't get codigo and nombre of all items.
I leave the JSON that I'm trying to get:
{"categorias":
{"cfcd208495d565ef66e7dff9f98764da":{"orden":0,"codigo":"001","nombre":"TUBO Y ACCESORIO DE COBRE, LATON"},
"c4ca4238a0b923820dcc509a6f75849b":{"orden":1,"codigo":"002","nombre":"TUBO Y ACCESORIO PVC PRESION"},
"c81e728d9d4c2f636f067f89cc14862c":{"orden":2,"codigo":"003","nombre":"AISLAMIENTO"},
"eccbc87e4b5ce2fe28308fd9f2a7baf3":{"orden":3,"codigo":"004","nombre":"MONTAJE DE AGUA SANITARIA"},
"a87ff679a2f3e71d9181a67b7542122c":{"orden":4,"codigo":"005","nombre":"ABRAZADERAS FONTANERIA"},"e4da3b7fbbce2345d7772b0674a318d5":{"orden":5,"codigo":"006","nombre":"FLEXOS DE ACERO"},
"1679091c5a880faf6fb5e6087eb1b2dc":{"orden":6,"codigo":"007","nombre":"ACCESORIOS DE SANEAMIENTO"},
...
...
...
This is my code on Android, and I always get null response:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
System.out.println("Respuesta:"+ response.toString());
try {
listaCategorias.clear();
int array=0;
JSONArray categorias = response.getJSONArray("categorias");
for (int i = 0; i < categorias.length(); i++) {
JSONObject objet = categorias.getJSONObject(i);
String titulo = objet.getString("nombre");
if (isIntegerParseInt(String.valueOf(titulo.charAt(0))))
listaCategorias.add(titulo);
}
array++;
onCallBack.onSuccess(listaCategorias);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("ErrorRespuesta: "+error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
its not an JSON ARRAY
Its JSON OBJECT
for your reference I'm posting sample json array
"three_day_forecast": [
{
"conditions": "Partly cloudy",
"day" : "Monday",
"temperature": 20
},
{
"conditions": "Showers",
"day" : "Tuesday",
"temperature": 22
},
{
"conditions": "Sunny",
"day" : "Wednesday",
"temperature": 28
}
]
Data was used by this reference.
It's not an Array but a JSONObject you need to modify your JSON to get an array with [] and not {}. Or you get it by getJSONObject.
With reference to the JSON String you've posted categorias is not a JSONArray. it's an object and the JSON string is still invalid.
On the top of the String you have the key as categories, now if you want to use that as an array you must have the value of categories to array which is specified within [ ] in JSON strings.
Another thing is try to avoid random key of the JSON Array if you're using it within loops with indexes. So your JSON String might look like following to support your Android code.
{
"categories": [
{ object 1... },
{ object 2... },
and so on...
]
}
You can see the type of JSON you will need here:
http://json-parser.com/53b37f69
I am developiing a android application for the blind and i used the Karios api and everything has worked out great
And i get the output of the api as
{
"images": [
{
"time": 0.86679,
"status": "Complete",
"file": "face_57078c26c5baf.jpg",
"width": 776,
"height": 570,
"faces": [
{
"topLeftX": 94,
"topLeftY": 95,
"width": 189,
"height": 189,
"leftEyeCenterX": 152.275,
"leftEyeCenterY": 172.175,
"rightEyeCenterX": 225.5125,
"rightEyeCenterY": 168.2375,
"noseTipX": 188.83536147895,
"noseTipY": 209.93069059821,
"noseBtwEyesX": 188.69509888285,
"noseBtwEyesY": 163.76829692552,
"chinTipX": 194.90835566598,
"chinTipY": 272.94500076858,
"leftEyeCornerLeftX": 139.748828125,
"leftEyeCornerLeftY": 173.99609375,
"leftEyeCornerRightX": 167.2744140625,
"leftEyeCornerRightY": 173.6638671875,
"rightEyeCornerLeftX": 210.7591796875,
"rightEyeCornerLeftY": 171.3259765625,
"rightEyeCornerRightX": 236.955859375,
"rightEyeCornerRightY": 167.622265625,
"rightEarTragusX": -1,
"rightEarTragusY": -1,
"leftEarTragusX": -1,
"leftEarTragusY": -1,
"leftEyeBrowLeftX": 124.96433022747,
"leftEyeBrowLeftY": 160.66254675843,
"leftEyeBrowMiddleX": 144.98960402463,
"leftEyeBrowMiddleY": 151.01911416052,
"leftEyeBrowRightX": 171.91583787312,
"leftEyeBrowRightY": 155.84335467715,
"rightEyeBrowLeftX": 204.04144319738,
"rightEyeBrowLeftY": 153.33575045711,
"rightEyeBrowMiddleX": 228.74564647717,
"rightEyeBrowMiddleY": 144.48143172972,
"rightEyeBrowRightX": 247.64047899459,
"rightEyeBrowRightY": 149.93257330135,
"nostrilLeftHoleBottomX": 179.40484698779,
"nostrilLeftHoleBottomY": 222.21013668835,
"nostrilRightHoleBottomX": 200.05702183911,
"nostrilRightHoleBottomY": 220.59810540404,
"nostrilLeftSideX": 168.63097309489,
"nostrilLeftSideY": 217.27943710651,
"nostrilRightSideX": 211.08266584481,
"nostrilRightSideY": 213.96581724432,
"lipCornerLeftX": 164.95407560776,
"lipCornerLeftY": 244.11611360475,
"lipLineMiddleX": 192.40075144939,
"lipLineMiddleY": 240.81939551421,
"lipCornerRightX": 219.84742729102,
"lipCornerRightY": 237.52267742366,
"pitch": -1.1481443688526,
"yaw": 0.95481944449499,
"roll": -3.7557902314114,
"attributes": {
"gender": {
"time": 0.06871,
"type": "F",
"confidence": "90%"
}
}
}
]
}
]
}
How can I get to the type value of a face in android? Thanks in advance.
It should be something like this...
private String returnPersonType(String output) {
String type = "";
try {
JSONObject jsonObject = new JSONObject(output);
if (jsonObject.has("images")) {
JSONArray imagesArray = jsonObject.getJSONArray("images");
if (imagesArray.length() > 0 && imagesArray.getJSONObject(0).has("faces")) {
JSONArray facesArray = imagesArray.getJSONObject(0).getJSONArray("faces");
if (facesArray.length() > 0 && facesArray.getJSONObject(0).has("attributes") && facesArray.getJSONObject(0).getJSONObject("attributes").has("gender")) {
type = facesArray.getJSONObject(0).getJSONObject("attributes").getJSONObject("gender").getString("type");
}
}
}
} catch (JSONException e) {
}
return type;
}
In above response
faces is a JSONArray with one value
Get JSONArray of faces
Then Get JSONObject from faces JSONArray of 0 index and get values that you need.
First thing to keep in mind while parsing
{- JSONObject function needs to be called
[- JSONArray function needs to be called
here is the solution to your problem if you only want to reach faces[0] object..
{
....
....
JSONObject jsonRootObject=new JSONObject("/*Your son data as string or any string variable containing your son data*/");
JSONArray jsonArray=jsonRootObject.optJSONArray("images");
JSONObject object0=jsonArray.optJSONObject(0);
JSONArray faces=object0.optJSONArray("faces");
JSONObject face0=faces.optJSONObject(0);
//Now you can call any object data by using its key. following code gets height stored int it
String t=face0.optString("height");
...
...
}
... represents your code which you will write as per your need.
Good luck with your programming.
There is one simple Solution to get it easily.
Convert your Json with This link will convert your json to POJO class
you will get some classes as result.
And by following demo code in your onPostExecute() method , you can get Faces Data,
String json = new Gson().toJson(object);
YourMainPOJO newProjectList = new Gson().fromJson(json,
YourMainPOJO.class);
ArrayList<YourImagePOJO> cpImageData = cp.getImages();
ArrayList<YourFacePOJO> cpFaceData = cpImageData. getFaces();
String topLeftY = cpFaceData.getTopLeftY ();
String topLeftX = cpFaceData.getTopLeftX ();
This is the simple way you can get your result easily, without any hassle.
res.images[0].faces[0].attributes.gender.type
Assuming your response is in res
Sorry for my English, I can't parse json object like this:
{
"user": {
"id": 34,
"last_login": {
"date": "2015-07-30 11:22:34.000000",
"timezone_type": 3,
"timezone": "Europe/Oslo"
}
}
}
I don't know, why it's not working. I need parse last_login, but I can't do this
My code
JSONObject jsonTable = json.getJSONObject("user");
if(jsonTable.length() > 0) {
check = true;
ProfileDetalisObject pfDet = new ProfileDetalisObject();
pfDet.setId(jsonTable.getString(TAG_ID));
JSONObject date = jsonTable.getJSONObject("last_login");
if(date.length() > 0) {
Iterator<String> iteratorDate = date.keys();
while( iteratorDate.hasNext() ) {
String currentKeySensor = iteratorDate.next();
JSONObject objDate = date.optJSONObject(currentKeySensor);
if(objDate != null) {
pfDet.setDate(objDate.getString("date"));
pfDet.setTimezone_type(objDate.getString("timezone_type"));
pfDet.setTimezone(objDate.getString("timezone"));
}
}
}
I can't parse last_login.
last_login is normal json object (aka dictionary), so you do not need any iterators, just reach the keys directly:
JSONObject date = jsonTable.getJSONObject("last_login");
pfDet.setDate(date.getString("date"));
pfDet.setTimezone_type(date.getString("timezone_type"));
pfDet.setTimezone(date.getString("timezone"));
This question already has answers here:
How to call the JSON object in Android
(5 answers)
Closed 8 years ago.
i am trying to use one json file in my project.i have one Json file in following structure,i succeed to access json array "business", but now i want to access business_cat and in business_cat, cat 1 and cat 2. how can i access these values?
{
"business":[
{
"id":"13",
"category":"Dinner",
"subcategory":"",
"name_eng":"dinner 1",
"name_arab":"dinner 1",
"mobile":"12345",
"address":"not now",
"logo":"1.gif",
"contact":"Call",
"open_time":" ",
"close_time":" "
}
],
"business_cat":[
[
"cat 1",
{
"name":"dish 1",
"id":"7",
"name_arab":"dish1",
"price":"200",
"logo":"laptop.jpeg"
},
{
"name":"dish 2",
"id":"8",
"name_arab":"dish 2",
"price":"123789",
"logo":"micky.jpg"
},
{
"name":"qaz",
"id":"10",
"name_arab":"zaq",
"price":"12",
"logo":"watch.jpg"
},
{
"name":"wsx",
"id":"11",
"name_arab":"xsw",
"price":"12",
"logo":"micky.jpg"
},
{
"name":"zxc",
"id":"12",
"name_arab":"vcxz",
"price":"34",
"logo":"camera.jpg"
}
],
[
"cat2",
{
"name":"d1",
"id":"9",
"name_arab":"d1",
"price":"300",
"logo":"watch.jpg"
}
]
]
}
A JSON Object starts with a { and ends with a } while a JSON Array starts with a [ and ends with a ].
The whole string will be JSONObject, from which each array can be extracted via json.getJSONArray(ARRAYNAME)
JSONObject json = new JSONObject(jsonString);
JSONArray business = json.getJSONArray("business");
//business
System.out.println("*****business*****"+business.length());
for(int i=0;i<business.length();i++){
JSONObject json_data_business = business.getJSONObject(i);
Log.i("log","id"+json_data_business.getInt("id")+
", category"+json_data_business.getString("category")
);
// business_cat
JSONArray business_cat = json.getJSONArray("business_cat");
System.out.println("*****business_cat*****"+business_cat.length());
for(int i=0;i<business_cat.length();i++){
JSONObject json_data_business_cat = business_cat.getJSONObject(i);
Log.i("log","id"+json_data_business_cat.getInt("id")+
", name"+json_data_business_cat.getString("name")
);
}
JSONObject obj = new JSONObject(content);
if (obj != null) {
Iterator<?> it = obj.keys();
while (it.hasNext()) {
String key = it.next().toString();
JSONArray value = (JSONArray) obj.optString(key);
---then you parse the JSONArray
}
}
How I filter this JSONArray that the JSONObject "name" contain "abcd"
{
"items":[{
"id":"082111",
"name":"abcd efgh"
}, {
"id":"082112",
"name":"abcd klmn"
}, {
"id":"082113",
"name":"klmn efgh"
}, {
"id":"082114",
"name":"abcd efgh"
}, {
"id":"082115",
"name":"efgh oprs"
}
]
}
And the result is must be
{
"items":[{
"id":"082111",
"name":"abcd efgh"
}, {
"id":"082112",
"name":"abcd klmn"
}, {
"id":"082114",
"name":"abcd efgh"
}
]
}
How I can get the result like that?
Should I convert the JSONArray to ArrayList, and filter when have converted to ArrayList, and convert again to JSONArray?
If yes, how i convert the JSONArray to ArrayList and filter it? And conver again to JSONArray?
Please give me samples code.
With the following code you'll get that which ever you required that is which are starting with the letters abcd like as you said.
JSONObject json = new JSONObject(jsonString);
JSONArray jData = json.getJSONArray("items");
for (int i = 0; i < jData.length(); i++) {
JSONObject jo = jData.getJSONObject(i);
if ((jo.getString("name")).startsWith("abcd", 0)) {
Log.i("Name is ", jo.getString("name"));
}
}