JSON解析Android的 - 嵌套的项目? [英] JSON Parsing for Android - nested items?

查看:130
本文介绍了JSON解析Android的 - 嵌套的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用这个网站多年,但从来没有公布。我难倒,并希望有人也许能帮助我。

I have used this site for years, but never have posted. I am stumped and hope someone might be able to help me.

我使用非常相似的code到什么Slicekick张贴在这里 JSON分析应用[否数据"?,但似乎无法弄清楚如何解析这个JSON文件。我已经编辑了JSON的信息,以反映我试图解析,以节省空间/时间的确切格式。我如何解析结果这样我就可以查询名称和类型?像他有问题的类似和信息,我怎么解析信息和下面的文件中结果?

I am using very similar code to what Slicekick posted here JSON parsing app "no data"?, but can not seem to figure out how to parse this JSON file. I have edited his JSON info to reflect the exact format I am trying to parse to save space/time. How do I parse the "Results" so I can query "Name" and "Type"? Like his problem with "Similar" and "Info", how do I parse "Info" and "Results" in the file below?

下面是我使用的确切格式编辑的JSON文件的例子:

Here is an example of a JSON file edited in the exact format I am using:

{
"head": {
    "title": "Music",
    "status": "200"
},
"Info": [
    {
        "Name": "Mos Def",
        "Type": "music",
        "Results": [
            {
                "Name": "Talib Kweli",
                "Type": "music"
            },
            {
                "Name": "Black Star",
                "Type": "music"
            },
            {
                "Name": "Little Brother",
                "Type": "music"
            }
         ]
    }, 
    {
        "Name": "Mos Def",
        "Type": "Vehicles",
        "Results": [
            {
                "Name": "Chevy",
                "Type": "Car"
            },
            {
                "Name": "Ford",
                "Type": "Car"
            },
            {
                "Name": "Pontiac",
                "Type": "Car"
            }
         ]
      }
   ]
}

这可能是我的兴趣code的部分是:

The part of my code that may be of interest is:

...我做了HTTPGET
...这被建成一个StringBuilder
......与StringBuilder的结果将创建一个JSONObject

... I do a httpget ... that gets built into a StringBuilder ... creates a JSONObject with the results of StringBuilder

   jArray = new JSONObject(result);

...然后返回

... then returns that

然后到...

   JSONArray  Info = json.optJSONArray("Info");

   System.out.println("HERE IS INFO: ");
   System.out.println(Info);
   //System.out.println("HERE IS RESULTS: ");
   //System.out.println(Results);

和基本上这里就是我难倒。
我把打印消息,试图缩小问题了。

And basically here is where I am stumped. I put in print messages to try to narrow the issue down.

解析信息让我来搜索:姓名:莫斯戴夫类型:音乐 - 和 - 名称:莫斯戴夫类型:车辆

Parsing "Info" allows me to search: "Name": "Mos Def" "Type": "music" -and- "Name": "Mos Def" "Type": "Vehicles"

与结果更换搜索信息的给了我没有数据。 (未找到)

Replacing the search of "Info" with "Results" gives me no data. (Not found)

任何想法?

推荐答案

您需要打电话给你解析code如下:

You need to call your parsing code as below:

jArray = new JSONObject(result);
for(int i=0;i<jArray.length();i++)
{
    JSONArray  Info = json.optJSONArray("Info");
    for(int j=0;j<Info.length();j++){
    {
         System.out.println("HERE IS INFO: ");
         System.out.println(Info);
         //System.out.println("HERE IS RESULTS: ");
         JSONObject obj=Info.getJSONObject(j);
         JSONArray results=obj.getJSONArray("Results");
         for(int k=0; k<results.length();
         {
             //Process Results
         }
}

这篇关于JSON解析Android的 - 嵌套的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆