0

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

我现在不知道为什么或我有什么问题。它几乎没有变化......?请看看我的方法,看看你的想法。谢谢

片段活动:

@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);
    }

解析器类:

    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 到日志的错误:

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);
}
4

2 回答 2

1

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

 ["foo":bar]

如果它是一个对象,它看起来像这样:

 {"foo":bar}

您尝试做的第一件事是:

 new JSONObject(jsonString);

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

 new JSONArray(jsonString);

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

于 2012-06-08T23:30:03.293 回答
1

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

页面包含一些有关 JSON 数据格式的有用(且简洁!)信息,这些信息将帮助指导您使用 JSON 库。

于 2012-06-08T23:37:41.883 回答