android中的JSON解析-来自资源文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10479328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:54:56  来源:igfitidea点击:

JSON Parsing in android - from resource folder

androidjson

提问by Krishna Suthar

I've a kept one text file in res/rawfolder in eclipse. I am showing here the content of that file:

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

I want to parse this JSON array. How can I do this?

我想解析这个 JSON 数组。我怎样才能做到这一点?

Can anybody please help me??

有人可以帮我吗??

Thanks in advance.

提前致谢。

回答by Deval Patel

//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();
}

回答by StErMi

This is your code:

这是你的代码:

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
            }

回答by Gennadiy Ryabkin

Android framework has a helper JsonReaderin android.util package

Android 框架在 android.util 包中有一个辅助JsonReader

Works like a charm, presented great example. In first block small error with absent square bracket '}':

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

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

Also exists great library from google-devs GSON, which gives you possibility to map json structure straight to Java model: take a look here

还存在来自 google-devs GSON 的很棒的库,它使您可以将 json 结构直接映射到 Java 模型:看这里

回答by avimak

  1. Read it into a String
  2. Create a JSONArraywith the retrieved String
  3. Use get() methods to retrieve its data
  1. 读成一个 String
  2. JSONArray用检索到的创建一个String
  3. 使用 get() 方法检索其数据

回答by Jay Mayu

  1. Load the JASON string to a variable from the Res foler
  2. Parse it in your program
  1. 将 JASON 字符串加载到 Res 文件夹中的变量
  2. 在你的程序中解析它

I'm sure google will help you alot in this regard.

我相信谷歌会在这方面帮助你很多。

Anyway Good Luck.

总之祝你好运。