简体   繁体   中英

JSONException : at jsonData of type java.lang.String cannot be converted to JSONArray

As title said I'm new to JSON and I can't get out of my problem. I'm working on this error for 1 week I'm really desperate to get out.

My error :

JSONException: Value [{"data":"11-13-2017","numeVanzator":"Clau","numarClient":0}] at jsonData of type java.lang.String cannot be converted to JSONArray

My JSON :

{
 "jsonData" : {
    "11-13-2017" : {
      "Clau" : {
        "-KyokKjL9UQpsfKZYZqM" : [ {
          "pret" : "80",
          "produs" : "Shirt",
          "produsId" : "-Kyok58s0dOAnVOnbJPk"
        } ]
      }
    }
  }
}

I looked over tutorials on StackOverFlow but absolutely no solution.

My code :

 private void writeJSON(String metodaPlata) throws JSONException {
    String numeVanzator = SharedPreference.getString(this, SharedPreference.USER_DATA, SharedPreference.NUME_VANZATOR, "");
    String jsonDataFromShared = SharedPreference.getString(this, SharedPreference.APP_DATA, SharedPreference.JSON_DATA, "");

    int totalPrice = 0;
    for(VanzatorProduse v : Util.getInstance().getVanzatorProduse())
    {
        int vPrice = Integer.parseInt(v.getPret());
        totalPrice = totalPrice + vPrice;

    }
    String pretTotal = Integer.toString(totalPrice);
    String produseSelectate = Integer.toString(listaProdusePreview.getAdapter().getCount());


    JSONObject jsonData;
    JSONArray dateJSON;
    JSONObject obj;
    JSONArray arrayForList;


    if (jsonDataFromShared.equals("")) {

        jsonData = new JSONObject();
        dateJSON = new JSONArray();
        obj = new JSONObject();
        arrayForList = new JSONArray();

        JSONObject objListaSiModalitate = new JSONObject();

        //       arrayForList.put(stock_list.toString());

        objListaSiModalitate.put("lista", new JSONArray(Util.getInstance().getVanzatorProduse()));
        objListaSiModalitate.put("metodaPlata", metodaPlata);

        obj.put("data", getDate(calendarData.getTimeInMillis()));
        obj.put("numeVanzator", numeVanzator);
        obj.put("numarClient", numarVanzare);

        dateJSON.put(obj);
        jsonData.put("jsonData", dateJSON.toString());

        SharedPreference.putString(this, SharedPreference.APP_DATA, SharedPreference.JSON_DATA, jsonData.toString());

    } else {

        jsonData = new JSONObject(jsonDataFromShared);
        dateJSON = jsonData.getJSONArray("jsonData");

        obj = new JSONObject();

        JSONObject objListaSiModalitate = new JSONObject();
        objListaSiModalitate.put("metodaPlata", metodaPlata);
        obj.put("produseSelectate", produseSelectate);
        obj.put("sumaProduse", pretTotal);
        obj.put("data", getDate(calendarData.getTimeInMillis()));
        obj.put("numeVanzator", numeVanzator);
        obj.put("numarClient", numarVanzare);

        dateJSON.put(obj);

        jsonData.put("jsonData", dateJSON);

        System.out.println("jsonData" + dateJSON);
        SharedPreference.putString(this, SharedPreference.APP_DATA, SharedPreference.JSON_DATA, jsonData.toString());


    }
}

Please help me I have no idea what to idea even if I look over tutorials.

I think your json parsing will be,

try {
            JSONObject jsonObject = new JSONObject("jsonData");
            JSONObject jsonObject1 = jsonObject.getJSONObject("11-13-2017");
            JSONObject jsonObject2 = jsonObject1.getJSONObject("Clau");
            JSONArray jsonArray = jsonObject2.getJSONArray("-KyokKjL9UQpsfKZYZqM");
            JSONObject jsonObject3 = (JSONObject) jsonArray.getJSONObject(0);
            String string = jsonObject3.getString("pret");
            String string1 = jsonObject3.getString("produs");
            String string2 = jsonObject3.getString("produsId");
        } catch (JSONException e) {
            e.printStackTrace();
        }

Error causes from this line

jsonData.put("jsonData", dateJSON.toString()); 

Here dateJSON.toString() store the string value to jsonData variable. jsonData is JsonObject.

Then You try to retrieve JsonArray from string

jsonData = new JSONObject(jsonDataFromShared); //This line convert your string to jsonobject.
dateJSON = jsonData.getJSONArray("jsonData"); 

EDITED

If you want to retreive array from dateJson object Replace this line

jsonData.put("jsonData", dateJSON.toString()); 

To

jsonData.put("jsonData", dateJSON); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM