jsonarray无法转换为jsonobject logcat错误

时间:2013-10-10 09:54:08

标签: android json arrays jsonobject

Oke我得到了几个JSONArrays,我已将其作为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;
    }

这是我尝试用它来获取其中一个数组的代码:

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}]}]

这是我从文件中获取的字符串,所以我必须将其作为onject而不是数组放置?

  

所以你需要

     

JSONArray jr = new JSONArray(“jsonstring”);所以替换

     

JSONObject jOb = new JSONObject(json);由

     

JSONArray jr = new 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。

2 个答案:

答案 0 :(得分:2)

根据你的logcat,你的json就像下面的

 [  // 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
            }
        ]
    }
]

[表示json数组节点

{表示json对象节点

所以你需要

 JSONArray jr = new JSONArray("jsonstring");

所以替换

 JSONObject jOb = new JSONObject(json);

通过

JSONArray jr = new JSONArray(json);

答案 1 :(得分:1)

我认为第一个问题是你认为你拥有的例子数据与你真正拥有的例子数据有关。

您展示了包含数组数据的字段的JSONObject数据示例,但在createJSONExampleDataFile(JSONArray jArray)中,您正在将JSONArray写为顶级项而非JSONObject。稍后当您阅读它时,您正在尝试阅读JSONObject但您之前写的是JSONArray,因此您收到的错误无法将JSONArray转换为JSONObject 1}}。

因此修复将修改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中,根据您的原始数据绑定到字段名称,这使得以后可以使用相同的命名字段检索它们。 / p>