在android-from资源文件夹中进行JSON解析[英] JSON Parsing in android - from resource folder

本文是小编为大家收集整理的关于在android-from资源文件夹中进行JSON解析的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在Eclipse中保存了一个在 res/grent>文件夹中的文本文件.我在这里显示该文件的内容:

{
    "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数组.我该怎么办?

有人可以帮我吗?

提前感谢.

推荐答案

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

其他推荐答案

这是您的代码:

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
            }

其他推荐答案

android框架有一个助手 jsonreader 在Android.util包中

像魅力一样,呈现出优秀的例子.在第一个块小错误中,没有方形括号'}':

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 Model:看看这里

本文地址:https://www.itbaoku.cn/post/102333.html

问题描述

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

{
    "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?

Can anybody please help me??

Thanks in advance.

推荐答案

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

其他推荐答案

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
            }

其他推荐答案

Android framework has a helper JsonReader in android.util package

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