i have the below json string,
"objects": [{
"d430f6c0-a293-4fb9-86fe-a163618cf180": {
"id": "d430f6c0-a293-4fb9-86fe-a163618cf180",
"in_use": 0,
"alterable": 1,
"comment": ""
},
"123-a293-4fb9-86fe-a163618cf180": {
"id": "123-a293-4fb9-86fe-a163618cf180",
"in_use": 2,
"alterable": 3,
"comment": "dfgfg"
}
}]
and cant figure out a way to parse it, all objects in the array have the id in front as shown above.
Thanks for the help in advance.
Firstly your json array is in wrong format you can check it jsonviewer
your json data will be like this:
{"objects": [{
"d430f6c0-a293-4fb9-86fe-a163618cf180": {
"id": "d430f6c0-a293-4fb9-86fe-a163618cf180",
"in_use": 0,
"alterable": 1,
"comment": ""
},
"123-a293-4fb9-86fe-a163618cf180": {
"id": "123-a293-4fb9-86fe-a163618cf180",
"in_use": 2,
"alterable": 3,
"comment": "dfgfg"
}
}]}
now for parsing this data you can use this method:
public void parseData(String jsonObjectData) {
try {
JSONObject jsonObject = new JSONObject(jsonObjectData);
JSONArray jsonArray = jsonObject.getJSONArray("objects");
for (int i = 0 ; i <jsonArray.length(); i ++)
{
String id = jsonArray.getJSONObject(i).getJSONObject("d430f6c0-a293-4fb9-86fe-a163618cf180").getString("id");
Log.d("id", id);
}
} catch (Exception e) {
}
}
you have to get json object like this String id = jsonArray.getJSONObject(i).getJSONObject("d430f6c0-a293-4fb9-86fe-a163618cf180").getString("id");
it will provide single object data, you cannot fetch all because your json is not in proper format.
Related
Format of json data inside file is like,
{
"data": {
"id": 0,
"amount": 0,
"no": 0
}
}
I need to edit id of this file.
I used following snippet to do so,
String configFileString = getStringFromFile(configFile.toString());
JSONObject json = new JSONObject(configFileString);
json.put("id", 2);
JSONObject gg = new JSONObject();
gg.put("id",json);
writeJsonFile(configFile, gg.toString());
But the output is like,
{
"data": {
"data": {
"id": 0,
"amount": 0,
"no": 0
},
"id": 2
}
}
What modifications should I do in my code? Please help me with this
String configFileString = getStringFromFile(configFile.toString());
JSONObject json = new JSONObject(configFileString);
json.getJSONObject("data").put("id", 2);
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
I can't seem to understand how to parse the "dispay_sizes" JSON array.
Here is my approach so far.
This is the JSON
{
"result_count": 6577719,
"images": [
{
"id": "505093872",
"asset_family": "creative",
"caption": "Professional footballer kicking ball during game in floodlit soccer stadium full of spectators under stormy sky and floodlights. It's snowing up there.",
"collection_code": "EPL",
"collection_id": 712,
"collection_name": "E+",
"display_sizes":
[{
"is_watermarked": false,
"name": "thumb",
"uri": "http://cache4.asset-cache.net/xt/505093872.jpg?v=1&g=fs1|0|EPL|93|872&s=1&b=RjI4"
}],
"license_model": "royaltyfree",
"max_dimensions": {
"height": 6888,
"width": 8918
},
"title": "Soccer player kicking a ball"
},
My code to parse it:
JSONObject imageData = new JSONObject(jsonData);
int resultCount = imageData.optInt("result_count");
Log.d("Result Count: ",Integer.toString(resultCount));
JSONArray imageArray = imageData.optJSONArray("images");
for( int i = 0; i < imageArray.length(); i++)
{
JSONObject img= imageArray.getJSONObject(i);
String id = img.optString("id");
allID.add(id);
Log.d("Image ID: ", id);
JSONArray imagePath = img.getJSONArray("display_sizes");
String pathURI = imagePath.getString("uri");
Log.d("Image Path: ",pathURI);
}
Can someone explain how to parse JSON array within a JSON array?
This
JSONArray imagePath = img.getJSONArray("display_sizes");
is right
[ // represents json array
{ // jsonobject
"is_watermarked": false,
"name": "thumb",
"uri": "http://cache4.asset-cache.net/xt/505093872.jpg?v=1&g=fs1|0|EPL|93|872&s=1&b=RjI4"
you have array of json object.
So you need
for(int j=0;j<imagePath.length();j++)
{
JSONObject jb = imagePath.getJSONObject(j);
String pathURI = jb.getString("uri");
}
I am sending request to the server and it gives me a response and giving data in JSON formats, now i want to fetch some specific value from that JSON format, so hot to do it.
{
"education": [
{
"school": {
"id": "2009305",
"name": "FG boys public high school Bannu Cantt "
},
"type": "High School"
},
{
"school": {
"id": "109989",
"name": "University of Engineering & Technology"
},
"type": "College"
}
],
"id": "xxxxxxx"
}
Now i need the school names from this xml,
Try this..
You response is in Json format not xml.
JSONObject json = new JSONObject(response);
JSONArray education = json.getJSONArray("education");
for(int i = 0; i < education.length(); i++){
JSONObject con_json = education.getJSONObject(i);
String school_type = con_json.getString("type");
JSONObject school_json = con_json.getJSONObject("school");
String school_name = school_json.getString("name");
}
It is not XML. it is completely in Json Standard format.
first build a JSONobject from your data:
JSONObject jsonObj = new JSONObject(result); //result = your Data String in fetched from server
then you cab retrieve what you want using its key. for example:
jsonObj.getString("id"); // it returns "xxxxxxx". as is in your data
I am trying to access the "display_name" object within the following JSON and I cannot seem to grab it. The example JSON is below.
{
"streams": [
{
"broadcaster": "fme",
"_id": 5019229776,
"preview": "http://static-cdn.jtvnw.net/previews-ttv/live_user_zisss-320x200.jpg",
"game": "Diablo III",
"channel": {
"mature": null,
"background": "http://static-cdn.jtvnw.net/jtv_user_pictures/zisss-channel_background_image-06a9d8c1113e5b45.jpeg",
"updated_at": "2013-03-04T05:27:27Z",
"_id": 31795858,
"status": "Barb sets giveaway and making 500m DH set... Join Zisspire, earn Zeny, collect prizes!",
"logo": "http://static-cdn.jtvnw.net/jtv_user_pictures/zisss-profile_image-502d7c865c5e3a54-300x300.jpeg",
"teams": [ ],
"url": "http://www.twitch.tv/zisss",
"display_name": "Zisss",
"game": "Diablo III",
"banner": "http://static-cdn.jtvnw.net/jtv_user_pictures/zisss-channel_header_image-997348d7f0658115-640x125.jpeg",
"name": "zisss",
"video_banner": null,
"_links": {
"chat": "https://api.twitch.tv/kraken/chat/zisss",
"subscriptions": "https://api.twitch.tv/kraken/channels/zisss/subscriptions",
"features": "https://api.twitch.tv/kraken/channels/zisss/features",
"commercial": "https://api.twitch.tv/kraken/channels/zisss/commercial",
"stream_key": "https://api.twitch.tv/kraken/channels/zisss/stream_key",
"editors": "https://api.twitch.tv/kraken/channels/zisss/editors",
"videos": "https://api.twitch.tv/kraken/channels/zisss/videos",
"self": "https://api.twitch.tv/kraken/channels/zisss",
"follows": "https://api.twitch.tv/kraken/channels/zisss/follows"
},
"created_at": "2012-07-01T21:09:58Z"
},
"name": "live_user_zisss",
"viewers": 775,
"_links": {
"self": "https://api.twitch.tv/kraken/streams/zisss"
}
}
],
"_links": {
"summary": "https://api.twitch.tv/kraken/streams/summary",
"followed": "https://api.twitch.tv/kraken/streams/followed",
"next": "https://api.twitch.tv/kraken/streams?channel=zisss%2Cvoyboy&game=Diablo+III&limit=100&offset=100",
"featured": "https://api.twitch.tv/kraken/streams/featured",
"self": "https://api.twitch.tv/kraken/streams?channel=zisss%2Cvoyboy&game=Diablo+III&limit=100&offset=0"
}
I start with:
JSONArray array = getJSONArray("streams");
JSONObject object = array.getJSONObject(4); // channel is entry 4 in array
String name = object.getString("display_name");
I'm not sure what I'm doing wrong here. With a more filled out JSON with multiple "channel" entries, I'm not sure how to handle it. I was thinking something like this?
String[] name = new String[array.length()];
JSONArray array = getJSONArray("streams");
for(int i = 0; i < array.length(); i++) {
if(array[i].equals("channel")
name[i] = array.getString("display_name");
I'm sure that last part is crude, and probably quite off from what it should be, but I'm not sure how to handle this.
Based on your JSON you have to do the following
JSONArray array = getJSONArray("streams");
JSONObject object = array.getJSONObject(0);
object = object.getJSONOBject("channel")
String name = object.getString("display_name");
The first item in you json array holds the channel object which contains the data you are looking for.
Channel is not the 4th item in your JSONArray but a nested object within the first. You have to be careful to follow the [] and {} brackets when parsing JSON because JSONObjects can contain multiple nested sub-objects and arrays.
JSONObject mainJsonObject = new JSONObject(json_string);
JSONArray array = mainJsonObject.getJSONArray("streams");
JSONObject channelObject = array.getJSONObject(0).getJSONOBject("channel")
String displayName = channelObject.getString("display_name");