Here is my JSON object and i want to parse it in android project
{
"second": {
"versionInfo": "0.20.3-dev",
"compileDate": "Mon Mar 12 17:39:23 IST 2012",
"compileUser": "suraj",
"trackname": "tracker_localhost:localhost/127.0.0.1:48418"
},
"href": {
"versionInfo": "null",
"compileDate": "null",
"compileUser": "null",
"trackname": "null"
},
"first": {
"key": "['trackname','versionInfo','compileDate','compileUser']"
}
}
How to compile?
i want to first extract attributes of 'first' and using the attributes and then to extract parameters of 'second' using attributes of 'first'.
Basically it is done like that:
JSONObject jobj = new JSONObject(theString);
JSONObject first = jobj.getJSONObject("first");
JSONObject second = jobj.getJSONObject("second");
If you want more, take a look at the documentation of JSON classes for android.
Edit
Regarding the extraction of the array (in first->key):
String jStr = first.getString("key");
JSONArray jArr = new JSONArray(jStr);
Related
I have a JSONObject. I need to append some details to that.
Existing JSON
{
"class": {
"name": "first",
"language": "English"
}
}
and i need to append values to this like
{
"class": [{
"name": "first",
"language": "English"
}, {
"name": "first",
"language": "English"
}]
}
I'm not the first to ever say this,
But you should use JSONObject.put with the modified value, e.g
JSONObject main = new JSONObject();
// ...
main.put("Classes", newClasses);
You can't do that with your current object. You have to change the type of the JSONObject you're appending to. As noted in the comments, you actually just want to serialize an array of custom objects.
Therefore, create a JsonArray as described in this answer: JSONObject.append into object - result is nested array?
Java is not my first language, but maybe this will help you out:
//First create the name and language object
JSONObject nameLanguageObj = new JSONObject();
nameLanguageObj.add("name","first");
nameLanguageObj.add("language","English");
//Create the array to hold the objects
JSONArray arr = new JSONArray();
arr.put(nameLanguage);
//Create the full class object as you requested.
JSONObject classObj = new JSONObject();
classObj.put("class", arr);
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
So I have a json response from web service like this
{
error: false
types: [2]
-0: {
card_type_id: 5
category: "Food & Beverages"
}
-1: {
card_type_id: 8
category: "Entertaiment"
}
}
what I want to do is to get the number inside json array "types" and use it to looping to set the value of arraylist that will be use in listview, but my code seems and cannot get the json array length, this is my code
JSONObject obj = new JSONObject(response);
if(!obj.getBoolean("error")){
JSONArray types = obj.getJSONArray("types");
for(int i=0; i<types.length();i++)
{
JSONObject a = types.getJSONObject(i);
int ct_id = a.getInt("card_type_id");
String category = a.getString("category");
CardType ct = new CardType();
ct.setTypeId(_ct_id);
ct.setTypeCategory(_category);
categoryArray.add(ct);
}
}
Can anyone help me? Thanks before
This is what your JSON response probably should look like (note also that keys and string values should be sorrounded by double-quotes):
{
"error": false,
"types": [
{
"card_type_id": 5,
"category": "Food & Beverages"
},
{
"card_type_id": 8,
"category": "Entertaiment"
}
]
}
The JSONArray "types" in this example has a length (types.length()) of 2. Now you can implement your for-loop and should get the expected results.
actually it's just my fault to see the json from advanced rest client, i use the json menu and the app automatically correct the json and become invalid, after i change to raw menu i can see the actual json and correct the code
I have JSON data like this:
"books": [
{
"rating": {},
"subtitle": "",
"author": [
"The OReilly Java Authors"
],
"pubdate": "2003-9",
"tags": [],
"origin_title": "",
…… …… …… …… … …
and I want get the author data,some "author"data has more than one author,but I just confused weather I could parse it like this:
JSONArray jsonbooks = mess.getJSONArray("books");
for(blabla){
JSONObject obj = jsonbooks.getJSONObject(i);
obj.getJSONArray("author").get(1):
or
JSONArray jsonbooks = mess.getJSONArray("books");
for(blabla){
JSONObject obj = jsonbooks.getJSONObject(i);
obj.getString("author");
sencond way,I could get the data like"["author,blabla "],I want get rid off [" "],and I need more string processing,if there a better way looks like the first way?
First one is right just one change obj.getJSONArray("author").getString(0):
JSONArray jsonbooks = mess.getJSONArray("books");
for(blabla){
JSONObject obj = jsonbooks.getJSONObject(i);
obj.getJSONArray("author").getString(0):
Since author is an array therefore first convert that into jsonArray
JSONArray jsonAuthor = mess.getJSONArray("author");
then String author = jsonAuthor.get(0); //this will give you -> The OReilly Java Authors
Hope this helps:
JSONArray jsonAuthorNode = booksJsonObject.optJSONArray("author");
int jsonArrLen = jsonAuthorNode.length();
for(int j=0;j<jsonArrLen;j++)
{
String authorName = jsonAuthorNode.get(j);
}
I have a JSON as shown below:
{
"places": [{
"name": "Ankola",
"slug": "ankola",
"category": "beach",
"distance": "521",
"travel_time": "8 hrs, 2 mins",
"days": "3",
"latitude": "14.669456",
"longitude": "74.300952",
"weather": "Summer 21\u00b0-36\u00b0C, Winter 12\u00b0-35\u00b0C",
"todo": "Baskal gudda, Nadibag, Shedikuli, Keni, Belekeri",
"about": "Ankola is a small town surrounded by numerous temples. It is in line with Arabian sea. Ankola is famous for its native breed of mango called ishaad and for cashews harvesting.",
"image": [
"Cm5NXlq.jpg",
"9OrlQ9C.jpg",
"DRWZakh.jpg",
"dFKVgXA.jpg",
"5WO2nDf.jpg"
]
}]
}
I know how to fetch key - value pairs, but i dont know how to parse array inside json to form a string array(image - in my case)
To summarize i want something like this:
I have 5 image names under "image" tag, i want them in a string array. How can i do this?
Use :
JSONArray images = yourJSONObject.getJSONArray("image");
for(int i = 0; i < images.length(); i++){
String image = images.getString(i);
}
This should do the trick as I remember.
Here you go:
JSONArray ja = whatEverYourJsonObject.getJSONArray("image");
for(int i=0; i<ja.length(); j++){
String name = ja.getString(i);
}
You first have to convert the JSON string to a Java object (JSONObject). Then, you obtain your JSONArray and iterate over it.
Example:
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject (jsonString);
JSONArray images = itemObj.getJSONArray ("images");
int length = images.length ();
for (int i = 0; i < length; i++)
Log.d ("Image Filename", images.getString (i));
} catch (JSONException e) {
e.printStackTrace();
}
EDIT: Now I see that your JSON is invalid - you have an object for each image and that object contains only the value part of the data. An example of a valid images array would be as follows:
{
"image": [
"Cm5NXlq.jpg",
"9OrlQ9C.jpg",
"DRWZakh.jpg",
"dFKVgXA.jpg",
"5WO2nDf.jpg"
]
}
I would suggest:
Create your own class which describes your data structure defined
by those json object. As a last resort you can even generate Java
class based on JSON string - look at
jsongen
When you will have your own Java class (let's say MyClass) you can easily parse JSON to your generated Java class using GSON, like:
MyClass myClass = gson.fromJson(jsonString, MyClass.class);