2

わかりました私はJSONオブジェクトとしてJSONArrayに追加し、ファイルに保存したいくつかのJSONArrayを取得しました。

これは私のjsonステートメントです:

{
    "wines": [
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 0 is a very sweet red wine.",
            "name": "Wine number: 0",
            "barcode": 1234567890123,
            "year": 1982,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        },
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 1 is a very sweet red wine.",
            "name": "Wine number: 1",
            "barcode": 1234568019470,
            "year": 1983,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        },
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 2 is a very sweet red wine.",
            "name": "Wine number: 2",
            "barcode": 1234568148817,
            "year": 1984,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        },
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 3 is a very sweet red wine.",
            "name": "Wine number: 3",
            "barcode": 1234568278164,
            "year": 1985,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        },
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 4 is a very sweet red wine.",
            "name": "Wine number: 4",
            "barcode": 1234568407511,
            "year": 1986,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        },
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 5 is a very sweet red wine.",
            "name": "Wine number: 5",
            "barcode": 1234568536858,
            "year": 1987,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        },
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 6 is a very sweet red wine.",
            "name": "Wine number: 6",
            "barcode": 1234568666205,
            "year": 1988,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        },
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 7 is a very sweet red wine.",
            "name": "Wine number: 7",
            "barcode": 1234568795552,
            "year": 1989,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        },
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 8 is a very sweet red wine.",
            "name": "Wine number: 8",
            "barcode": 1234568924899,
            "year": 1990,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        },
        {
            "biologic": 0,
            "sparkling": 1,
            "color": "Red",
            "description": "This wine number 9 is a very sweet red wine.",
            "name": "Wine number: 9",
            "barcode": 1234569054246,
            "year": 1991,
            "barcodeType": "EAN13",
            "taste": "Sweet",
            "country": "Netherlands"
        }
    ],
    "reviewWine": [
        {
            "reviewIDWine": 0,
            "wineID": 1234567890123,
            "reviewWine": "This wine is not that sweet at all.ksbdksbcksdbcksdcbsdcbsdc,bskdcbksdckscbs ,scscb",
            "dateReviewWine": 1380876068189,
        "titleReviewWine": "Not sweet"
        },
        {
            "reviewIDWine": 1,
            "wineID": 56121,
            "reviewWine": "This wine is not that sweet at all.",
            "dateReviewWine": 1340826068189,
        "titleReviewWine": "Not sweet"
        },
        {
            "reviewIDWine": 2,
            "wineID": 56162,
            "reviewWine": "This wine is not that sweet at all.",
            "dateReviewWine": 1386876068189,
        "titleReviewWine": "Not sweet"
        }
    ],
"reviewStore": [
        {
            "reviewIDStore": 0,
            "reviewStoreID": 1234567890123,
            "reviewStore": "This wine is not that sweet at all.ksbdksbcksdbcksdcbsdcbsdc,bskdcbksdckscbs ,scscb",
            "dateReviewStore": 1380876068189,
        "TitleReviewStore": "Not sweet"
        },
        {
            "reviewIDStore": 1,
            "reviewStoreID": 56121,
            "reviewStore": "This wine is not that sweet at all.",
            "dateReviewStore": 1340826068189,
        "TitleReviewStore": "Not sweet"
        },
        {
            "reviewIDStore": 2,
            "reviewStoreID":  56162,
            "reviewStore": "This wine is not that sweet at all.",
            "dateReviewStore": 1386876068189,
        "TitleReviewStore": "Not sweet"
        }
    ],
    "ratingWine": [
        {
            "ratingIDWine": 0,
            "wineID": 1234567890123,
            "ratingWine": 9,
            "dateAddedRatingWine": 1380876008180,
        "userIDAddedRatingWine": 4
         },
        {
            "ratingID": 1,
            "wineID": 56162,
            "ratingWine": 9,
            "dateAddedRatingWine": 1300876068189,
        "userIDAddedRatingWine": 4
        },
        {
            "ratingID": 2,
            "wineID": 1234567890123,
            "ratingWine": 9,
            "dateAddedRatingWine": 1380800068189,
        "userIDAddedRatingWine": 4
        }
    ],
    "user": [
        {
            "userID": 0,
            "userName": "test0",
            "password": "test0",
            "email": "cyber@gmail.com"
        },
        {
            "userID": 1,
            "userName": "test1",
            "password": "test1",
            "email": "claw@gmail.com"
        },
        {
            "userID": 2,
            "userName": "test2",
            "password": "test2",
            "email": "cyber2k@gmail.com"
        },
        {
            "userID": 3,
            "userName": "test3",
            "password": "test3",
            "email": "cyber2000@gmail.com"
        }
    ],
    "store": [
        {
            "storeID": 0,
            "storeName": "Edah helmond",
            "city": "helmond",
            "streetName": "fretlaan",
            "houseNumber": "90",
        "country":"Netherlands"
        },
        {
            "storeID": 1,
            "storeName": "ah helmond",
            "city": "helmond",
            "streetName": "de byrdstraat",
            "houseNumber": "9c",
        "country":"Netherlands"
        },
        {
            "storeID": 2,
            "storeName": "Edah eindhoven",
            "city": "eindhoven",
            "streetName": "de haag",
            "houseNumber": "90a",
        "country":"Netherlands"
        },
        {
            "storeID": 3,
            "storeName": "Edah eden",
            "city": "eden",
            "streetName": "de laan",
            "houseNumber": "10",
        "country":"Netherlands"
        },
        {
            "storeID": 4,
            "storeName": "ah neunen",
            "city": "neunen",
            "streetName": "de haaglaan",
            "houseNumber": "9",
        "country":"Netherlands"
        }
    ],
    "wineStoreRelation": [
        {
            "wineID": 0,
            "storeID": 2,
            "price": "4.59"
        },
        {
            "wineID": 2,
            "storeID": 1,
            "price": "13.98"
        },
        {
            "wineID": 2,
            "storeID": 2,
            "price": "3.55"
        },
        {
            "wineID": 1,
            "storeID": 1,
            "price": "3.50"
        },
        {
            "wineID": 0,
            "storeID": 1,
            "price": "6.50"
        },
        {
            "wineID": 1,
            "storeID": 0,
            "price": "3.50"
        }
    ]
}

これは、ファイルを作成するために配列を追加するコードです:

public void createJSONExampleDataFile(JSONArray jArray) {


        File f = new File(fileJSON);
        f.delete();
        if (!f.exists()) {
            try {
                FileWriter fos = new FileWriter(f);
                fos.write(jArray.toString());
                fos.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                f.delete();
                FileWriter fos = new FileWriter(f);
                fos.write(jArray.toString());
                fos.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
public JSONArray createJSONExampleData() {
        JSONArray ar = new JSONArray();

        ar.put(createJSONWineExampleData());
        ar.put(createJSONReviewExampleData());
        return ar;
    }

    public JSONObject createJSONWineExampleData() {
        winesJSONArray = new JSONArray();
        JSONObject o= new JSONObject(); 

        for (int i = 0; i < 10; i++) {
            jObj = new JSONObject();
            try {
                jObj.put(JSON_WINE_BARCODE, Long.valueOf((1234567890123L + (i * 129347))));
                jObj.put(JSON_WINE_BARCODETYPE, "EAN13");
                jObj.put(JSON_WINE_NAME, "Wine number: " + i);
                jObj.put(JSON_WINE_YEAR, (1982 + i));
                jObj.put(JSON_WINE_COUNTRY, "Netherlands");
                jObj.put(JSON_WINE_TASTE, "Sweet");
                jObj.put(JSON_WINE_COLOR, "Red");
                jObj.put(JSON_WINE_DESCRIPTION, "This wine number " + i + " is a very sweet red wine.");
                jObj.put(JSON_WINE_SPARKLING, 1);
                jObj.put(JSON_WINE_BIOLOGIC, 0);

                System.out.println(jObj.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            winesJSONArray.put(jObj);
        }
        try {
            o.put(JSON_ARRAY_WINE, winesJSONArray);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return o;
    }

    public JSONObject createJSONReviewExampleData() {
        reviewWineJSONArray = new JSONArray();
        JSONObject o= new JSONObject(); 

        for (int i = 0; i < 5; i++) {
            jObj = new JSONObject();
            try {
                jObj.put(JSON_REVIEWWINE_REVIEWID, 1+i);
                jObj.put(JSON_REVIEWWINE_WINEID, Long.valueOf((1234567890123L + (i * 129347))));
                jObj.put(JSON_REVIEWWINE_REVIEW, i);
                jObj.put(JSON_REVIEWWINE_DATE, 1340826068189L-(i*12999823));
                jObj.put(JSON_REVIEWWINE_TITLE, "SWeet red: "+i);

                System.out.println(jObj.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            reviewWineJSONArray.put(jObj);
        }
        try {
            o.put(JSON_ARRAY_REVIEWWINE, reviewWineJSONArray);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return o;
    }

これは、配列の1つを取得するために試したコードです。

public JSONObject getJSONObjectWineFromJSONObjectArray(JSONObject jsonOb, String barcode) {
        JSONArray jsonAr = new JSONArray();

        try {
            jsonAr = jsonOb.getJSONArray(JSON_ARRAY_WINE);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        for (int i = 0; i < jsonAr.length(); i++) {
            try {
                jsonAr.getJSONObject(i);
                if (jsonAr.getJSONObject(i).getString(JSON_WINE_BARCODE).equals(barcode)) {
                    return jsonAr.getJSONObject(i);
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

public JSONArray getJSONArrayWinesFromFile() {

        try {
            is = new FileInputStream(new File(fileJSON));
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            System.out.println("Dit is de string: "+json.toString());

        } catch (FileNotFoundException e) {
            Log.e("File Error", "Error getting file result " + e.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        try {
            JSONObject jOb = new JSONObject(json);

            winesJSONArray = jOb.getJSONArray(JSON_ARRAY_WINE);

            System.out.println("Lets see: "+winesJSONArray.toString(1));


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return winesJSONArray;
    }

これは私が得たlogcatです:

10-10 11:50:25.298: W/System.err(3548): org.json.JSONException: Value [{"wines":[{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 0 is a very sweet red wine.","name":"Wine number: 0","barcode":1234567890123,"year":1982,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 1 is a very sweet red wine.","name":"Wine number: 1","barcode":1234568019470,"year":1983,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 2 is a very sweet red wine.","name":"Wine number: 2","barcode":1234568148817,"year":1984,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 3 is a very sweet red wine.","name":"Wine number: 3","barcode":1234568278164,"year":1985,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 4 is a very sweet red wine.","name":"Wine number: 4","barcode":1234568407511,"year":1986,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 5 is a very sweet red wine.","name":"Wine number: 5","barcode":1234568536858,"year":1987,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 6 is a very sweet red wine.","name":"Wine number: 6","barcode":1234568666205,"year":1988,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 7 is a very sweet red wine.","name":"Wine number: 7","barcode":1234568795552,"year":1989,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 8 is a very sweet red wine.","name":"Wine number: 8","barcode":1234568924899,"year":1990,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 9 is a very sweet red wine.","name":"Wine number: 9","barcode":1234569054246,"year":1991,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"}]},{"reviewWine":[{"reviewIDWine":1,"dateReviewWine":1340826068189,"wineId":1234567890123,"titleReviewWine":"SWeet red: 0","reviewWine":0},{"reviewIDWine":2,"dateReviewWine":1340813068366,"wineId":1234568019470,"titleReviewWine":"SWeet red: 1","reviewWine":1},{"reviewIDWine":3,"dateReviewWine":1340800068543,"wineId":1234568148817,"titleReviewWine":"SWeet red: 2","reviewWine":2},{"reviewIDWine":4,"dateReviewWine":1340787068720,"wineId":1234568278164,"titleReviewWine":"SWeet red: 3","reviewWine":3},{"reviewIDWine":5,"dateReviewWine":1340774068897,"wineId":1234568407511,"titleReviewWine":"SWeet red: 4","reviewWine":4}]}] of type org.json.JSONArray cannot be converted to JSONObject
10-10 11:50:25.308: W/System.err(3548):     at org.json.JSON.typeMismatch(JSON.java:107)
10-10 11:50:25.308: W/System.err(3548):     at org.json.JSONObject.<init>(JSONObject.java:158)
10-10 11:50:25.308: W/System.err(3548):     at org.json.JSONObject.<init>(JSONObject.java:171)
10-10 11:50:25.308: W/System.err(3548):     at com.moyoweb.winescanner.JSONParser.getJSONArrayWinesFromFile(JSONParser.java:176)
10-10 11:50:25.308: W/System.err(3548):     at com.moyoweb.winescanner.ListWineView.onCreate(ListWineView.java:58)
10-10 11:50:25.308: W/System.err(3548):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
10-10 11:50:25.308: W/System.err(3548):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
10-10 11:50:25.308: W/System.err(3548):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
10-10 11:50:25.308: W/System.err(3548):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
10-10 11:50:25.308: W/System.err(3548):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
10-10 11:50:25.308: W/System.err(3548):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 11:50:25.308: W/System.err(3548):     at android.os.Looper.loop(Looper.java:150)
10-10 11:50:25.308: W/System.err(3548):     at android.app.ActivityThread.main(ActivityThread.java:4385)
10-10 11:50:25.308: W/System.err(3548):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 11:50:25.308: W/System.err(3548):     at java.lang.reflect.Method.invoke(Method.java:507)
10-10 11:50:25.308: W/System.err(3548):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
10-10 11:50:25.308: W/System.err(3548):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
10-10 11:50:25.308: W/System.err(3548):     at dalvik.system.NativeStart.main(Native Method)
10-10 11:50:25.318: D/AndroidRuntime(3548): Shutting down VM
10-10 11:50:25.318: W/dalvikvm(3548): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
10-10 11:50:25.318: E/AndroidRuntime(3548): FATAL EXCEPTION: main
10-10 11:50:25.318: E/AndroidRuntime(3548): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.moyoweb.winescanner/com.moyoweb.winescanner.ListWineView}: java.lang.NullPointerException
10-10 11:50:25.318: E/AndroidRuntime(3548):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at android.os.Looper.loop(Looper.java:150)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at android.app.ActivityThread.main(ActivityThread.java:4385)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at java.lang.reflect.Method.invoke(Method.java:507)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at dalvik.system.NativeStart.main(Native Method)
10-10 11:50:25.318: E/AndroidRuntime(3548): Caused by: java.lang.NullPointerException
10-10 11:50:25.318: E/AndroidRuntime(3548):     at com.moyoweb.winescanner.Wine.fromJSON(Wine.java:70)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at com.moyoweb.winescanner.ListWineView.onCreate(ListWineView.java:58)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
10-10 11:50:25.318: E/AndroidRuntime(3548):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
10-10 11:50:25.318: E/AndroidRuntime(3548):     ... 11 more

10-10 11:50:25.258: I/System.out(3548): Dit is de string: [{"wines":[{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 0 is a very sweet red wine.","name":"Wine number: 0","barcode":1234567890123,"year":1982,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 1 is a very sweet red wine.","name":"Wine number: 1","barcode":1234568019470,"year":1983,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 2 is a very sweet red wine.","name":"Wine number: 2","barcode":1234568148817,"year":1984,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 3 is a very sweet red wine.","name":"Wine number: 3","barcode":1234568278164,"year":1985,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 4 is a very sweet red wine.","name":"Wine number: 4","barcode":1234568407511,"year":1986,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 5 is a very sweet red wine.","name":"Wine number: 5","barcode":1234568536858,"year":1987,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 6 is a very sweet red wine.","name":"Wine number: 6","barcode":1234568666205,"year":1988,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 7 is a very sweet red wine.","name":"Wine number: 7","barcode":1234568795552,"year":1989,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 8 is a very sweet red wine.","name":"Wine number: 8","barcode":1234568924899,"year":1990,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"},{"biologic":0,"sparkling":1,"color":"Red","description":"This wine number 9 is a very sweet red wine.","name":"Wine number: 9","barcode":1234569054246,"year":1991,"barcodeType":"EAN13","taste":"Sweet","country":"Netherlands"}]},{"reviewWine":[{"dateReviewWine":1340826068189,"reviewIDWine":1,"titleReviewWine":"SWeet red: 0","wineId":1234567890123,"reviewWine":0},{"dateReviewWine":1340813068366,"reviewIDWine":2,"titleReviewWine":"SWeet red: 1","wineId":1234568019470,"reviewWine":1},{"dateReviewWine":1340800068543,"reviewIDWine":3,"titleReviewWine":"SWeet red: 2","wineId":1234568148817,"reviewWine":2},{"dateReviewWine":1340787068720,"reviewIDWine":4,"titleReviewWine":"SWeet red: 3","wineId":1234568278164,"reviewWine":3},{"dateReviewWine":1340774068897,"reviewIDWine":5,"titleReviewWine":"SWeet red: 4","wineId":1234568407511,"reviewWine":4}]}]

それはファイルから取得した文字列なので、配列ではなくオブジェクトとして片付けなければなりませんか?

だからあなたが持っている必要があります

JSONArray jr = new JSONArray("jsonstring"); だから交換

JSONObject jOb = 新しい JSONObject(json); に

JSONArray jr = 新しい JSONArray(json);

私はこれを試しました:

try {
            winesJSONArray = new JSONArray(json);

            System.out.println(winesJSONArray.toString());


            System.out.println("Lets see: "+winesJSONArray.toString(1));

このlogcatを取得しました:

http://pastebin.com/WwZax22H制限のペーストビンとして追加。

4

2 に答える 2

1

最初の問題は、サンプルデータとして「考えている」ものと、サンプルデータとして「実際に」持っているものとの違いだと思います。

配列データを保持するフィールドを持つ JSONObject データの例を示していますが、あなたはをトップレベルのアイテムとしてcreateJSONExampleDataFile(JSONArray jArray)書き、ではありません。後で読み返すと、 a を読み込もうとしていますが、以前に書いたのは aであったため、に変換できないというエラーが表示されます。JSONArrayJSONObjectJSONObjectJSONArrayJSONArrayJSONObject

したがって、修正はcreateJSONExampleDataFileを受け入れるように変更しJSONObject、コードをリファクタリングして、最上位のオブジェクト タイプとして保存するものがJSONObject(複数のJSONArrayフィールド メンバーを含む)になるようにすることです。

だから、まず、

public JSONArray createJSONExampleData() {
    JSONArray ar = new JSONArray();

    ar.put(createJSONWineExampleData());
    ar.put(createJSONReviewExampleData());
    return ar;
}

このようなものでなければなりません...

public JSONObject createJSONExampleData() {
    JSONObject ar = new JSONObject();

    ar.put("wines",createJSONWineExampleData());
    ar.put("reviewWine",createJSONReviewExampleData());
    return ar;
}

つまり、元のデータに従ってフィールド名にバインドJSONArrayされた に格納されJSONObjectます。これにより、同じ名前のフィールドを使用して、後でそれらを取得できます。

于 2013-10-10T10:13:30.263 に答える