基本上我想做的是通过使用类别过滤来自foursquare的场地,但是我想让用户选择他们想要的那种“过滤器”,但到目前为止我无法正确解析json。 / p>
原始的json数据,例如
{
"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
并使用我现在拥有的代码如下:
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"));
}
}
从检查运行输出的txt文件开始,我的json如下:
"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"
},
并且从那里开始,json数据已经错了,因为json数据被削减得太多以至于我无法使用该名称,但我不知道我做错了什么,任何指针家伙?
答案 0 :(得分:1)
更改
int length = jsonObj.length();
到
int length = groups.length();
使用JsonArray
内的项目数而不是JsonObject
for for condition condition