Reputation: 171
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?
Upvotes: 1
Views: 957
Reputation: 132972
change
int length = jsonObj.length();
to
int length = groups.length();
use number of items inside JsonArray
instead of JsonObject
in for loop condition
Upvotes: 1