0

生のリソースフォルダーから取得した文字列に解析する 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);
    }

パーサー クラス:

    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 に答える