【问题标题】:JSON data to android applicationJSON数据到android应用程序
【发布时间】:2011-05-16 08:47:58
【问题描述】:

我在我的 json 对象中得到了这个部分。但我无法将其作为 java 列表。它给出错误。所以我试着把它当作一个字符串数组。但这和以前一样。那么我应该使用什么来解析这些数据以用于我的 android 应用程序?

cuisine: {
-cuisine_names: [
"All (36)"
"Malaysian/ Singaporean (1)"
"Asian (1)"
"Australian (2)"
"Chinese (1)"
"European (3)"
"Spanish (1)"
"Greek (2)"
"Steak House (1)"
"Indian (1)"
"International (7)"
"Thai (1)"
"Italian (8)"
"Modern Australian (7)"
]
-price_ranges: [
"Any Price"
"$0-15"
"$15-30"
"$30+"
]
-times: [
"Any Time"
"05:30PM"
"06:00PM"
"06:30PM"
"07:00PM"
"07:30PM"
"08:00PM"
"08:30PM"
"09:00PM"
"09:30PM"
"10:00PM"
"10:30PM"
"11:00PM"
"11:30PM"
]
}

提前致谢!

【问题讨论】:

    标签: android json collections


    【解决方案1】:

    要使用 JSONObject 填充列表,您应该使用这样的函数(其中 NewsBSR 是具有一些基本字段的自定义对象):

    private ArrayList<NewsBSR> getListObjectsNews(JSONObject objNews)
        {
            ArrayList<NewsBSR> listNews = new ArrayList<NewsBSR>();
    
            try{
                for (Iterator iterator = objNews.keys(); iterator.hasNext();) 
                {
                 String cle = String.valueOf(iterator.next());
    
                 Object objet = objNews.get(String.valueOf(cle));
                 Log.v("V", "News: "+cle+ " : "+objet.toString());
                 if (cle.equals("results"))
                 {
                     JSONArray array = objNews.getJSONArray(cle);
                     for(int i = 0; i < array.length() ; i++)
                     {
                         Object obj = array.get(i);
                         Iterator it = ((JSONObject) obj).keys();
                         NewsBSR news = new NewsBSR();
                         while (it.hasNext())
                         {
    
                             String k = String.valueOf(it.next());
                             String val = ((JSONObject) obj).getString(k);
                             Log.v("V", "Array content : "+k+ " : "+val);
                             if (k.equals("tt") &&  val.length() > 0)
                             {
                                 news.setTitle(val);
                             }
                             if (k.equals("dt") &&  val.length() > 0)
                             {
                                news.setDate(UtilsDate.stringToDate(val));
                             }
                             if (k.equals("num") &&  val.length() > 0)
                             {
                                 news.setId(val);
                             }
                         }
                         listNews.add(news);
                     }
                 }
                }
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                 Log.v("V", "Error HOME: "+ e.getMessage());
                e.printStackTrace();
            }
    
    
            return (listNews);
        }
    

    【讨论】:

      【解决方案2】:

      我想你忘记了逗号。你可以使用这个 http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

      【讨论】:

        【解决方案3】:

        json格式不应该是这样的。

        首先,这样的代码周围应该有一个根对象。

        { <---this
        cuisine: {
        -cuisine_names: [
        "All (36)"........
        } <-- and this
        

        然后你需要像这样在数组中的每个字符串之间使用逗号

        "Australian (2)",
        "Chinese (1)",
        "European (3)",
        "Spanish (1)",
        "Greek (2)",
        "Steak House (1)",
        

        【讨论】:

          【解决方案4】:

          用Gson很简单:

          public class Foo
          {
            static String jsonInput =
            "{" +
              "\"cuisine\": {" +
                "\"cuisine_names\": [" +
                  "\"All (36)\"," +
                  "\"Malaysian/ Singaporean (1)\"," +
                  "\"Asian (1)\"," +
                  "\"Australian (2)\"," +
                  "\"Chinese (1)\"," +
                  "\"European (3)\"," +
                  "\"Spanish (1)\"," +
                  "\"Greek (2)\"," +
                  "\"Steak House (1)\"," +
                  "\"Indian (1)\"," +
                  "\"International (7)\"," +
                  "\"Thai (1)\"," +
                  "\"Italian (8)\"," +
                  "\"Modern Australian (7)\"" +
                "]," +
                "\"price_ranges\": [" +
                  "\"Any Price\"," +
                  "\"$0-15\"," +
                  "\"$15-30\"," +
                  "\"$30+\"" +
                "]," +
                "\"times\": [" +
                  "\"Any Time\"," +
                  "\"05:30PM\"," +
                  "\"06:00PM\"," +
                  "\"06:30PM\"," +
                  "\"07:00PM\"," +
                  "\"07:30PM\"," +
                  "\"08:00PM\"," +
                  "\"08:30PM\"," +
                  "\"09:00PM\"," +
                  "\"09:30PM\"," +
                  "\"10:00PM\"," +
                  "\"10:30PM\"," +
                  "\"11:00PM\"," +
                  "\"11:30PM\"" +
                "]" +
              "}" + 
            "}";
          
            public static void main(String[] args)
            {
              GsonBuilder gsonBuilder = new GsonBuilder();
              Gson gson = gsonBuilder.create();
              CuisineContainer cc = gson.fromJson(jsonInput, CuisineContainer.class);
              System.out.println(cc);
            }
          }
          
          class CuisineContainer
          {
            private Cuisine cuisine;
          
            @Override
            public String toString()
            {
              return cuisine.toString();
            }
          }
          
          class Cuisine
          {
            private String[] cuisine_names;
            private String[] price_ranges;
            private String[] times;
          
            @Override
            public String toString()
            {
              StringBuilder result = new StringBuilder();
              result.append("cuisine_names: ");
              result.append(Arrays.asList(cuisine_names));
              result.append(System.getProperty("line.separator"));
              result.append("price_ranges: ");
              result.append(Arrays.asList(price_ranges));
              result.append(System.getProperty("line.separator"));
              result.append("times: ");
              result.append(Arrays.asList(times));
              return result.toString();
            }
          }

          输出:

          cuisine_names: [All (36), Malaysian/ Singaporean (1), Asian (1), Australian (2), Chinese (1), European (3), Spanish (1), Greek (2), Steak House (1), Indian (1), International (7), Thai (1), Italian (8), Modern Australian (7)]
          price_ranges: [Any Price, $0-15, $15-30, $30+]
          times: [Any Time, 05:30PM, 06:00PM, 06:30PM, 07:00PM, 07:30PM, 08:00PM, 08:30PM, 09:00PM, 09:30PM, 10:00PM, 10:30PM, 11:00PM, 11:30PM]

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-29
            • 2017-05-30
            • 1970-01-01
            • 2013-10-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多