Facebook Profile Information JSON Parsing in Android - android

For example : if the given below is json of the person's profile on facebook got thorugh facebook sdk login through android app , How we will get the School Name fron the education Field in th json data in android . Please Help
Data :
{
"id": "1464730016",
"name": "Ravi Tamada",
"first_name": "Ravi",
"last_name": "Tamada",
"link": "https://www.facebook.com/ravi8x",
"username": "ravi8x",
"birthday": "12/22/1988",
"hometown": {
"id": "112158005464147",
"name": "Baruva"
},
"location": {
"id": "102186159822587",
"name": "Chennai, Tamil Nadu"
},
"bio": "Author: www.androidhive.info\r\nCo-author: www.9lessons.info",
"work": [
{
"employer": {
"id": "179366562092719",
"name": "ByteAlly"
},
"location": {
"id": "102186159822587",
"name": "Chennai, Tamil Nadu"
},
"position": {
"id": "124917314217511",
"name": "Product Head"
}
]
}
],
"favorite_athletes": [
{
"id": "18620649907",
"name": "Virat Kohli"
}
],
"education": [
{
"school": {
"id": "131587206873093",
"name": "Raghu Engineering College (REC)"
},
"degree": {
"id": "140065339390579",
"name": "B.Tech"
},
"year": {
"id": "142963519060927",
"name": "2010"
},
"type": "Graduate School",
"classes": [
{
"id": "192259410803415",
"name": "2010",
"with": [
{
"id": "584960408",
"name": "Santosh Patnaik"
}
],
"from": {
"id": "584960408",
"name": "Santosh Patnaik"
}
}
]
}
],
"gender": "male",
"relationship_status": "Single",
"website": "www.androidhive.info\nwww.9lessons.info\nwww.twitter.com/ravitamada\nwww.about.me/rv",
"timezone": 5.5,
"locale": "en_US",
"languages": [
{
"id": "106059522759137",
"name": "English"
},
{
"id": "107617475934611",
"name": "Telugu"
},
{
"id": "112969428713061",
"name": "Hindi"
},
{
"id": "343306413260",
"name": "Tamil"
}
],
"verified": true,
"updated_time": "2012-03-02T17:04:18+0000"
}

JSONObject jsonResult = new JSONObject(jsonUser);
JSONArray data = jsonResult.getJSONArray("education");
if(data != null)
{
for(int i = 0 ; i < data.length() ; i++)
{
JSONObject c = data.getJSONObject(i);
String type = c.getString("type");
if(type.equalsIgnoreCase("college"))
{
JSONObject school = c.getJSONObject("school");
String id2 = school.getString("id");
String name2 = school.getString("name");
JSONObject year = c.getJSONObject("year");
String id_y = school.getString("id");
String name_y = school.getString("name");
}
}
}

Supposing that you've this json into a jsonObject that you retrieve as response, this is the way:
// Get jsonArray 'education' from main jsonObject
JSONArray jsonArrayEducation = jsonObject.getJSONArray("education");
JSONObject jsonSchool = jsonArrayEducation.getJSONObject("school");
Note that if you are interested only at the name, you can group the two lines above into
JSONObject jsonSchool = jsonObject.getJSONArray("education").getJSONObject("school");
// get school name
String schoolName = jsonSchool.getString("name");

Related

Dynamic object in json

I am working on a chat app and have a json response having dynamic objects in that, the dates in chat messages are dynamic. It changes. Please tell me how to make Model Class of this dynamic json. Please Help.
{
"result": 1,
"msg": "Data Found",
"data": {
"user_detail": {
"id": "3",
"first_name": "Rashi",
"last_name": "Singh",
"image_url": "https://res.cloudinary.com/dlnagpsst/image/upload/h_200,w_200/v1658072836/nassibo/9479.jpg"
},
"chat_messages": {
"2022-07-29": [
{
"id": "6",
"chat_id": "4",
"sender_id": "37",
"reciever_id": "3",
"message": "Hii",
"date": "2022-07-29",
"created_at": "2022-07-29 09:58:48",
"user_id": "37"
},
{
"id": "12",
"chat_id": "4",
"sender_id": "37",
"reciever_id": "3",
"message": "Kjhndkhjkdhjk",
"date": "2022-07-29",
"created_at": "2022-07-29 12:58:59",
"user_id": "37"
}
],
"2022-07-30": [
{
"id": "13",
"chat_id": "4",
"sender_id": "37",
"reciever_id": "3",
"message": "Hii",
"date": "2022-07-30",
"created_at": "2022-07-30 12:21:45",
"user_id": "37"
}
]
}
}
}
val b = response.getJSONObject("data").getJSONObject("chat_messages")
for (key in b.keys()) {
Log.i("///// keys",key.toString())
Log.i("///// keys_b",b[key].toString())
val child = b.getJSONArray(key)
for ( i in 0 until child.length()){
val subChild = JSONObject(child.getJSONObject(i).toString())
val id = subChild.getString("id")
val chat_id = subChild.getString("chat_id")
val sender_id = subChild.getString("sender_id")
val message = subChild.getString("message")
val user_id = subChild.getString("user_id")
Log.i("///// child_sub $i :",id)
Log.i("///// child_sub $i :",chat_id)
Log.i("///// child_sub $i :",sender_id)
Log.i("///// child_sub $i :",message)
Log.i("///// child_sub $i :",user_id)
}

How to get a given below value from json data?

I need to get the images array data how to put into the adapter and not required adapter code? Only loop and how to add that's important for me
and then after second I can access option_value array.
First all images can be accessed then after a option_value array.
{
"success": true,
"data": {
"id": "50",
"seo_h1": "",
"name": "Shirt 10001",
"manufacturer": "",
"sku": "",
"model": "10001",
"image": "http://api.yellowskydemo.com/image/cache/data/shirts/7228-500x500.jpg",
"images": [
"http://api.yellowskydemo.com/image/cache/data/shirts/13-500x500.jpg",
"http://api.yellowskydemo.com/image/cache/data/shirts/302-500x500.jpg",
"http://api.yellowskydemo.com/image/cache/data/shirts/5-500x500.jpg",
"http://api.yellowskydemo.com/image/cache/data/shirts/205-500x500.jpg"
],
"price": "$540.00",
"rating": 0,
"description": "<p>Fasten your fashion belts, for this is the ultimate edit of swanky casual wear by Ralph Lauren! For men who are born with a strong sense of style, the American powerhouse packs a colourful punch with this high-fash collection of shirts and sporty, monogrammed polos.</p>\r\n\r\n<p>Fasten your fashion belts, for this is the ultimate edit of swanky casual wear by Ralph Lauren! For men who are born with a strong sense of style, the American powerhouse packs a colourful punch with this high-fash collection of shirts and sporty, monogrammed polos.</p>\r\n",
"attribute_groups": [],
"special": "",
"discounts": [],
"options": [
{
"name": "Size",
"type": "select",
"option_value": [
{
"image": "http://api.yellowskydemo.com/image/cache/no_image-100x100.jpg",
"price": "$50.00",
"price_prefix": "-",
"product_option_value_id": "17",
"option_value_id": "55",
"name": "L-40",
"quantity": "99"
},
{
"image": "http://api.yellowskydemo.com/image/cache/no_image-100x100.jpg",
"price": "$40.00",
"price_prefix": "+",
"product_option_value_id": "18",
"option_value_id": "57",
"name": "XXL-44",
"quantity": "100"
}
],
"required": "1",
"product_option_id": "227",
"option_id": "14"
}
],
"minimum": "1",
"meta_description": "",
"meta_keyword": "",
"tag": "",
"upc": "",
"ean": "",
"jan": "",
"isbn": "",
"mpn": "",
"location": "",
"stock_status": "Out Of Stock",
"manufacturer_id": null,
"tax_class_id": "0",
"date_available": "2014-08-12",
"weight": "0.00000000",
"weight_class_id": "1",
"length": "0.00000000",
"width": "0.00000000",
"height": "0.00000000",
"length_class_id": "1",
"subtract": "0",
"sort_order": "1",
"status": "1",
"date_added": "2014-08-13 12:05:56",
"date_modified": "2015-06-30 11:19:39",
"viewed": "8",
"weight_class": "kg",
"length_class": "cm",
"reward": "0",
"points": "0",
"category": [
{
"name": "Shirts",
"id": "59"
},
{
"name": "Casual Shirts",
"id": "60"
}
],
"quantity": "99",
"reviews": {
"review_total": "0"
}
}
}
You should really look into How to parse JsonObject / JsonArray in android.
Please put your code of also what you have tried because people are here to help solve error/problem not to do coding.
Here is code from which you can Parse your json
JSONObject jsonObject = new JSONObject();
JSONObject dataObject = jsonObject.getJSONObject("data");
JSONArray imagesArray = dataObject.getJSONArray("images");
ArrayList<String> listOfImagesUrl = new ArrayList<>();
for(int i = 0; i < imagesArray.length(); i++)
{
listOfImagesUrl.add(imagesArray.getString(i)); // listOfImages
}
// code for option value
JSONArray optionsArray = dataObject.getJSONArray("options");
for(int i = 0; i < optionsArray.length(); i++)
{
JSONObject option = optionsArray.getJSONObject(i);
JSONArray optionsValueArray = option.getJSONArray("option_value");
for(int j = 0 ; j < optionsValueArray.length(); j++)
{
JSONObject optionValueObject = optionsValueArray.getJSONObject(j);
String image = optionValueObject.getString("image");
String price = optionValueObject.getString("price");
String price_prefix = optionValueObject.getString("price_prefix");
//same like this
}
}

How to loop through a dynamic json key in a nested json object in android

I am developing a quiz application and my questions are in a Json file
{
"name": "JHS ASANTE TWI",
"course_id": "73",
"courseID": "01JS88",
"package_code": "JHS",
"description": "",
"category": "",
"author": "content#exams.com",
"questions": {
"58242": {
"qid": "58242",
"topic": "1268",
"instructions": "Yi nea εne nea yεasensane aseε no asekyerε bɔ abira",
"text": "<p>Kofi de sika no maa <span style=\"text-decoration: underline;\">aberante<span>ε</span></span> bi.</p>",
"resource": "",
"qtype": "SINGLE",
"confirmed": "1",
"public": "1",
"flagged": "0",
"updated_at": "2014-07-10 12:29:33",
"rating": 0,
"answers": [
{
"id": "250310",
"text": "<p>ababaawa</p>",
"value": "1",
"solution": ""
},
{
"id": "250311",
"text": "<p>ab<span>ɔfra</span><span><br /></span></p>",
"value": "0",
"solution": ""
},
{
"id": "250312",
"text": "<p>aberewa</p>",
"value": "0",
"solution": ""
},
{
"id": "250313",
"text": "<p>abarimaa</p>",
"value": "0",
"solution": ""
}
]
},
"58245": {
"qid": "58245",
"topic": "1268",
"instructions": "Yi nea εne nea yεasensane aseε no asekyerε bɔ abira",
"text": "<p>Mehunuu m'adamfo bi nnora <span style=\"text-decoration: underline;\">an</span><span style=\"text-decoration: underline;\">ɔpa</span>.</p>\n<p> </p>\n<p> </p>",
"resource": "",
"qtype": "SINGLE",
"confirmed": "1",
"public": "1",
"flagged": "0",
"updated_at": "2014-07-10 12:43:29",
"rating": 0,
"answers": [
{
"id": "250329",
"text": "<p>awia</p>",
"value": "1",
"solution": ""
},
{
"id": "250328",
"text": "<p>anwummer<strong></strong>ε</p>",
"value": "0",
"solution": ""
},
{
"id": "250327",
"text": "<p>ahemadakye</p>",
"value": "0",
"solution": ""
},
{
"id": "250326",
"text": "<p>owigyinaeε</p>",
"value": "0",
"solution": ""
}
]
},
"58246": {
"qid": "58246",
"topic": "1268",
"instructions": "Yi nea εne nea yεasensane aseε no asekyerε bɔ abira",
"text": "<p>Asomaning <span style=\"text-decoration: underline;\">kuro</span> no so.</p>",
"resource": "",
"qtype": "SINGLE",
"confirmed": "1",
"public": "1",
"flagged": "0",
"updated_at": "2014-07-10 12:53:00",
"rating": 0,
"answers": [
{
"id": "250330",
"text": "<p><span>ɔmansini</span></p>",
"value": "0",
"solution": ""
},
{
"id": "250331",
"text": "<p><span>ɔman</span></p>",
"value": "0",
"solution": ""
},
{
"id": "250332",
"text": "<p>wiase</p>",
"value": "0",
"solution": ""
},
{
"id": "250333",
"text": "<p>akuraa</p>",
"value": "1",
"solution": ""
}
]
}
}
}
My question is: how do I access the next question of "questions" when i click next for the next question since "58242", "58245", etc are all dynamic values?
the answer on this post was very helpfull but now i want to now how loop through the questions How to parse a dynamic JSON key in a Nested JSON result
it helped me get the currentdynamic value but does not give me the next question when i click on next
private View.OnClickListener nextListener = new View.OnClickListener() {
public void onClick(View v) {
setAnswer();
try {
if (quesIndex != searchResult.getJSONObject("questions").length() - 1) {
quesIndex++;
progress.setProgress(0);
progress.setMax(100);
} else {
Intent myIntent = new Intent(TestActivity.this, Assesment.class);
myIntent.putExtra("intVariableName", score);
startActivity(myIntent);
}
} catch (JSONException e) {
e.printStackTrace();
}
try {
if (quesIndex == searchResult.getJSONObject("questions").length() - 1) {
next.setText("Finish");
Intent myIntent = new Intent(TestActivity.this, Assesment.class);
myIntent.putExtra("intVariableName", score);
startActivity(myIntent);
// close this activity
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
showQuestion(quesIndex, review);
}
};
will appreciate the help thanks :)
Not sure what you problem is. Seems like the solution is in the link you gave above.
JSONObject questions = result.getJSONObject("questions");
Iterator keys = questions.keys();
//go through every question
while (keys.hasNext()) {
String currentDynamicKey = (String)keys.next();
JSONObject currentQuestion = questions.getJSONObject(currentDynamicKey);
}

how to get facebook profile information using json in android

I want to get the name of school from this type of json :
"username": "blah",
"education": [
{
"school": {
"id": "[removed]",
"name": "[removed]"
},
"year": {
"id": "[removed]",
"name": "[removed]"
},
"type": "High School"
},
{
"school": {
"id": "[removed]",
"name": "[removed]"
},
"year": {
"id": "[removed]",
"name": "[removed]"
},
"type": "College"
}
]
here is my code :
JSONObject obj=null;
URL img_url;
String jsonUser = facebook.request("me");
obj=Util.parseJson(jsonUser);
String id=obj.optString("id");
String name=obj.optString("name");
what should i write the code to get the education fields school name ?
Facebook fb = new Facebook(API_KEY);// ... login user here ...JSONObject me = new JSONObject(fb.request("me")); String id = me.getString("id");
you can try
var schoolname = yourobject.education.school.name

how to parse foursquare category json

basically what i want to do is to filter venues from foursquare by using the categories but i i would want to let the user choose which kind of 'filter' they want, but so far i can't parse the json correctly.
the original json data eg
{
"meta": {
"code": 200
},
"response": {
"categories": [
{
"id": "4d4b7104d754a06370d81259",
"name": "Arts & Entertainment",
"pluralName": "Arts & Entertainment",
"shortName": "Arts & Entertainment",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/default_",
"suffix": ".png"
},
"categories": [
{
"id": "4fceea171983d5d06c3e9823",
"name": "Aquarium",
"pluralName": "Aquariums",
"shortName": "Aquarium",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/aquarium_",
"suffix": ".png"
},
"categories": [
]
},
{
"id": "4bf58dd8d48988d1e1931735",
"name": "Arcade",
"pluralName": "Arcades",
"shortName": "Arcade",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/arcade_",
"suffix": ".png"
},
"categories": [
]
},
{
"id": "4bf58dd8d48988d1e2931735",
"name": "Art Gallery",
"pluralName": "Art Galleries",
"shortName": "Art Gallery",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/artgallery_",
"suffix": ".png"
},
"categories": [
]
},
{
"id": "4bf58dd8d48988d1e4931735",
"name": "Bowling Alley",
"pluralName": "Bowling Alleys",
"shortName": "Bowling Alley",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/bowling_",
"suffix": ".png"
},
"categories": [
]
},
{
"id": "4bf58dd8d48988d17c941735",
"name": "Casino",
"pluralName": "Casinos",
"shortName": "Casino",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/casino_",
"suffix": ".png"
},
"categories": [
]
},
{
"id": "4bf58dd8d48988d18e941735",
"name": "Comedy Club",
"pluralName": "Comedy Clubs",
"shortName": "Comedy Club",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/comedyclub_",
"suffix": ".png"
},
"categories": [
]
bla bla bla
and using the code that i have now as below:
JSONObject jsonObj = new JSONObject(response);
jsonObj = jsonObj.getJSONObject("response");
JSONArray groups;// = (JSONArray) jsonObj.getJSONObject("response").getJSONArray("Categories);
groups = jsonObj.getJSONArray("categories");
FileOutputStream fos = WhereToEatLehActivity.this.openFileOutput("file_name"+".txt",Context.MODE_PRIVATE);
Writer out = new OutputStreamWriter(fos);
out.write(jsonObj.toString());
out.close();
int length = jsonObj.length();
if (length > 0) {
for (int i = 0; i < length; i++) {
JSONObject aCat = (JSONObject) groups.get(i);
Log.d("category", aCat.toString());
if(aCat.getString("name").equalsIgnoreCase("Food")){
Log.d("food", aCat.getString("id"));
}
}
and from checking the txt file that is output from the run, my json is as follows:
"categories": [
{
"id": "4d4b7104d754a06370d81259",
"icon": {
"suffix": ".png",
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/default_"
},
"categories": [
{
"id": "4fceea171983d5d06c3e9823",
"icon": {
"suffix": ".png",
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/aquarium_"
},
"categories": [
],
"shortName": "Aquarium",
"pluralName": "Aquariums",
"name": "Aquarium"
},
{
"id": "4bf58dd8d48988d1e1931735",
"icon": {
"suffix": ".png",
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/arcade_"
},
"categories": [
],
"shortName": "Arcade",
"pluralName": "Arcades",
"name": "Arcade"
},
and from there, the json data is wrong already as the json data is trimmed down too much that i cannot use the name but i don't know what am i doing wrongly, any pointer guys?
change
int length = jsonObj.length();
to
int length = groups.length();
use number of items inside JsonArray instead of JsonObject in for loop condition

Categories

Resources