1

基本上我想做的是通过使用类别从foursquare过滤场地,但我想让用户选择他们想要的那种“过滤器”,但到目前为止我无法正确解析json。

原始的 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数据被修剪得太多以至于我不能使用这个名字,但我不知道我做错了什么,任何指针?

4

1 回答 1

1

改变

 int length  = jsonObj.length();

 int length = groups.length();

使用里面的项目数JsonArray而不是JsonObjectfor 循环条件

于 2013-03-01T17:30:19.270 回答