移动到自己的Android类后JSON分析器错误

时间:2012-06-08 23:18:52

标签: android json parsing

我有一个JSONParser,它解析为从原始资源文件夹中提取的字符串。当它在一个活动中时,它工作得非常好。由于我需要在其他活动中重用它,我将它移动到它自己的类中。

我无法弄清楚为什么或我现在错了。它几乎不变......? 请看看我的方法,看看你的想法。日Thnx

片段活动:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        InputStream file = getResources().openRawResource(R.raw.regulatory_list);
        JSONParser jParser = new JSONParser();
        JSONArray json = jParser.getJSONFromFile(file);
        callback(jParser);
    }

The Parser Class:

    public class JSONParser {

    static JSONArray jArray = null;

    // constructor
    public JSONParser() {

    }

    public JSONArray getJSONFromFile(InputStream file) {

        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(file, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                file.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        String jsonString = writer.toString();

        // try parse the string to a JSON object
        try {
            jArray = new JSONArray(jsonString);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jArray;
    }

}

从JSONException到Log的错误:

06-08 16:04:21.504: E/JSON Parser(27082): Error parsing data org.json.JSONException: Value [{"title":"Advisory Circulators","label":"AC","date":"2008-03-03","_id":"1","gotoURL":null,"description":"Provides guidance such as methods, procedures, and practices for complying with regulations and requirements."},{"title":"Airworthiness Directives","label":"AD","date":"2012-06-08","_id":"2","gotoURL":"javascript:navClickListener('bodyContent', dns + '\/wiki\/index.php\/Airworthiness_Directive #content');","description":"Legally enforceable rules that apply to aircraft, aircraft engines, propellers, and appliances."},{"title":"Code of Federal Regulations","label":"CFR","date":"2012-01-31","_id":"3","gotoURL":"javascript:navClickListener('bodyContent',  dns + '\/wiki\/index.php\/Main_Page #content');","description":"Official Rulemaking documents of the CFR in Title 14 and have been published in the Federal Register"},{"title":"Parts Manufacturer Approvals","label":"PMA","date":"2012-01-31","_id":"4","gotoURL":null,"description":"Parts Manufacturer Approvals"},{"title":"Special Airworthiness Info Bulletins","label":"SAIB","date":"2012-01-31","_id":"5","gotoURL":null,"description":"Bulletins issued by manufacturers to provide modification or inspection instructions."},{"title":"Special Federal Aviation Regulation","label":"SFAR","date":"2012-01-31","_id":"6","gotoURL":null,"description":"Official Rulemaking documents that have changed the language of the CFR in Title 14 CFR for aviation."},{"title":"Supplemental Type Certificates","label":"STC","date":"2012-01-31","_id":"7","gotoURL":null,"description":"Document issued by the Federal Aviation Administration approving a product (aircraft, engine, or propeller) modification"},{"title":"Technical Standard Orders","label":"TSO","date":"2012-01-31","_id":"8","gotoURL":null,"description":"Minimum performance standards issued by the FAA for specified materials, parts, processes, and appliances used on civil aircraft."},{"title":"Type Certificate Data Sheets","label":"TCDS","date":"2012-01-31","_id":"9","gotoURL":null,"description":"Repository of Make and Model information of aircraft, engine or propeller including airspeed, weight, and thrust limitations, etc."}] of type org.json.JSONArray cannot be converted to JSONObject

编辑后记录:

06-08 16:44:42.744: E/JSON Parser(27515): Error parsing data org.json.JSONException: Value [{"title":"Advisory Circulators","label":"AC","date":"2008-03-03","_id":"1","gotoURL":null,"description":"Provides guidance such as methods, procedures, and practices for complying with regulations and requirements."},{"title":"Airworthiness Directives","label":"AD","date":"2012-06-08","_id":"2","gotoURL":"javascript:navClickListener('bodyContent', dns + '\/wiki\/index.php\/Airworthiness_Directive #content');","description":"Legally enforceable rules that apply to aircraft, aircraft engines, propellers, and appliances."},{"title":"Code of Federal Regulations","label":"CFR","date":"2012-01-31","_id":"3","gotoURL":"javascript:navClickListener('bodyContent',  dns + '\/wiki\/index.php\/Main_Page #content');","description":"Official Rulemaking documents of the CFR in Title 14 and have been published in the Federal Register"},{"title":"Parts Manufacturer Approvals","label":"PMA","date":"2012-01-31","_id":"4","gotoURL":null,"description":"Parts Manufacturer Approvals"},{"title":"Special Airworthiness Info Bulletins","label":"SAIB","date":"2012-01-31","_id":"5","gotoURL":null,"description":"Bulletins issued by manufacturers to provide modification or inspection instructions."},{"title":"Special Federal Aviation Regulation","label":"SFAR","date":"2012-01-31","_id":"6","gotoURL":null,"description":"Official Rulemaking documents that have changed the language of the CFR in Title 14 CFR for aviation."},{"title":"Supplemental Type Certificates","label":"STC","date":"2012-01-31","_id":"7","gotoURL":null,"description":"Document issued by the Federal Aviation Administration approving a product (aircraft, engine, or propeller) modification"},{"title":"Technical Standard Orders","label":"TSO","date":"2012-01-31","_id":"8","gotoURL":null,"description":"Minimum performance standards issued by the FAA for specified materials, parts, processes, and appliances used on civil aircraft."},{"title":"Type Certificate Data Sheets","label":"TCDS","date":"2012-01-31","_id":"9","gotoURL":null,"description":"Repository of Make and Model information of aircraft, engine or propeller including airspeed, weight, and thrust limitations, etc."}] of type org.json.JSONArray cannot be converted to JSONObject

有效的原始课程:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        InputStream is = getResources().openRawResource(R.raw.regulatory_list);
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        String jsonString = writer.toString();
        callback(jsonString);
}

2 个答案:

答案 0 :(得分:1)

从日志中显示你的JSON是一个数组,即它看起来像这样:

 ["foo":bar]

如果它是一个对象,它将如下所示:

 {"foo":bar}

你要做的第一件事是:

 new JSONObject(jsonString);

但它是一个阵列!所以你需要创建一个数组:

 new JSONArray(jsonString);

然后,您可以通过循环遍历数组来获取对象。

答案 1 :(得分:1)

您的异常表明您正在尝试从JSONArray的数据加载JSONObject。

这个page包含一些有用的(简明!)有关JSON数据格式的信息,这些信息将指导您使用JSON库。