2

好吧,我得到了几个 JSONArray,我将它们作为 JSONobject 添加到 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 = new 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添加为 pastebin cus of limit。

4

2 回答 2

2

According to your logcat your json is like below

 [  // json array node
    {  // json object node
        "wines": [  // json array wines
            {   // json object node
                "biologic": 0, // json string
                "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
            }
        ]
    }
]

[ represents json array node

{ represents json object node

So you need to have

 JSONArray jr = new JSONArray("jsonstring");

So replace

 JSONObject jOb = new JSONObject(json);

By

JSONArray jr = new JSONArray(json);
于 2013-10-10T10:04:19.800 回答
1

我认为第一个问题是您“认为”作为示例数据的内容与您“真正”拥有的作为示例数据的内容。

您展示了一个 JSONObject 数据的示例,其中包含保存数组数据的字段,但是在您createJSONExampleDataFile(JSONArray jArray)中,您将 aJSONArray作为顶级项目而不是JSONObject. 稍后当您阅读它时,您正在尝试阅读 aJSONObject但您之前写的是 a JSONArray,因此您会收到有关 cannot convert JSONArrayto a的错误JSONObject

因此,修复方法是修改createJSONExampleDataFile以接受 a JSONObject,并重构代码,以便您存储为最顶层对象类型的是 a 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;
}

即,JSONArrays 存储在 中JSONObject,绑定到字段名称,根据您的原始数据,这使得以后可以使用这些相同的命名字段检索它们。

于 2013-10-10T10:13:30.263 回答