【问题标题】:JSON Parsing in android - from resource folderandroid中的JSON解析-来自资源文件夹
【发布时间】:2012-05-07 09:00:12
【问题描述】:

我在 eclipse 的 res/raw 文件夹中保留了一个文本文件。我在这里显示该文件的内容:

{
    "Categories": {
        "Category": [
            {
                "cat_id": "3",
                "cat_name": "test"
            },
            {
                "cat_id": "4",
                "cat_name": "test1"
            },
            {
                "cat_id": "5",
                "cat_name": "test2"
            },
            {
                "cat_id": "6",
                "cat_name": "test3"
            }
        ]
    }
}

我想解析这个 JSON 数组。我该怎么做?

有人可以帮帮我吗?

提前致谢。

【问题讨论】:

标签: android json


【解决方案1】:
//Get Data From Text Resource File Contains Json Data.    
InputStream inputStream = getResources().openRawResource(R.raw.json);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

int ctr;
try {
    ctr = inputStream.read();
    while (ctr != -1) {
        byteArrayOutputStream.write(ctr);
        ctr = inputStream.read();
    }
    inputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}
Log.v("Text Data", byteArrayOutputStream.toString());
try {
    // Parse the data into jsonobject to get original data in form of json.
    JSONObject jObject = new JSONObject(
            byteArrayOutputStream.toString());
    JSONObject jObjectResult = jObject.getJSONObject("Categories");
    JSONArray jArray = jObjectResult.getJSONArray("Category");
    String cat_Id = "";
    String cat_name = "";
    ArrayList<String[]> data = new ArrayList<String[]>();
    for (int i = 0; i < jArray.length(); i++) {
        cat_Id = jArray.getJSONObject(i).getString("cat_id");
        cat_name = jArray.getJSONObject(i).getString("cat_name");
        Log.v("Cat ID", cat_Id);
        Log.v("Cat Name", cat_name);
        data.add(new String[] { cat_Id, cat_name });
    }
} catch (Exception e) {
    e.printStackTrace();
}

【讨论】:

  • "Categories" 是 Json 对象,"Category" 是包含所有记录的 Json 数组。
【解决方案2】:

这是你的代码:

String fileContent;
            JSONObject jobj = new JSONObject(fileContent);
            JSONObject categories = jobj.getJSONObject("Categories");
            JSONArray listCategory = categories.getJSONArray("Category");
            for( int i = 0; i < listCategory.length(); i++ ) {
                JSONObject entry = listCategory.getJSONObject(i);
                //DO STUFF
            }

【讨论】:

  • 我应该在fileContent中传递什么?是的,我知道它是字符串。但是在我的情况下如何传递这个字符串?请给我它的格式
  • fileContent 是 res/raw 中文件的内容。您还需要代码来加载它吗?
【解决方案3】:

Android 框架有一个助手JsonReader in android.util package

像魅力一样工作,提供了很好的例子。在没有方括号'}'的第一个块中出现小错误:

public List readJsonStream(InputStream in) throws IOException {
     JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
     try {
       return readMessagesArray(reader);
     } finally {
       reader.close();
     }
}

还有来自 google-devs GSON 的优秀库,它使您可以将 json 结构直接映射到 Java 模型:看看 here

【讨论】:

    【解决方案4】:
    1. 将其读入String
    2. 使用检索到的String 创建一个JSONArray
    3. 使用 get() 方法检索其数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      相关资源
      最近更新 更多