JSON 数据到 Android 应用程序

发布于 2024-11-07 07:14:29 字数 619 浏览 0 评论 0原文

我把这部分放在我的 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"
]
}

提前致谢 !

I got this part inside my json object. But I couldn't get this as a java List. It give error. So I tried to take it as a String Array. But it was same as before. So what should I use to parse this data to use with my android application ?

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"
]
}

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空名 2024-11-14 07:14:29

要使用 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);
    }

To fill a list with a JSONObject, you should use a function like this (where NewsBSR is a custom object with some basic fields):

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);
    }
枫林﹌晚霞¤ 2024-11-14 07:14:29

我想你忘记加逗号了。你可以用这个
http://www.androidcompetencycenter.com/2009/10/json-在android中解析/

I think you have forgotten put commas. And you can use this
http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

冧九 2024-11-14 07:14:29

json 格式不应该是这样的。

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

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

然后数组中的每个字符串之间都需要逗号,如下所示

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

The json format is not supposed to be like that.

first, there should be a root object surrounding the codes like this.

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

and then you need commas between every string in the array like so

"Australian (2)",
"Chinese (1)",
"European (3)",
"Spanish (1)",
"Greek (2)",
"Steak House (1)",
ま昔日黯然 2024-11-14 07:14:29

使用 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]

It's very simple with 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();
  }
}

output:

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文