JSONException:在java.lang.String类型的jsonData中无法转换为JSONArray

时间:2017-11-13 09:46:53

标签: java android json

正如标题所说,我是JSON的新手,我无法摆脱我的问题。我正在研究这个错误一周,我真的很想离开。

我的错误:

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

我的JSON

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

我查看了StackOverFlow上的教程,但绝对没有解决方案。

我的代码:

 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());


    }
}

请帮助我,即使我查看教程,我也不知道该怎么做。

2 个答案:

答案 0 :(得分:1)

我认为你的json解析将是,

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();
        }

答案 1 :(得分:1)

此行的错误原因

public class FormDesign
{
    public static FormDesign Dark = new FormDesign(0)
    {
        // Set Values
    };

    public static FormDesign Grey = new FormDesign(1)
    {
        // Set Values
    };

    public static FormDesign Light = new FormDesign(2)
    {
        // Set Values
    };

    private FormDesign(int i) { ID = i; }

    public FormDesign Switch()
    {
        if(this == Dark) return Grey;
        if (this == Grey) return Light;
        if (this == Light) return Dark;
        return Grey;
    }

    public int ID { get; private set; }
    public Color BackColor { get; private set; }
    public Color TitleColor { get; private set; }
    public Color TitleForeColor { get; private set; }
    public Color ForeColor { get; private set; }
    public Color LightColor { get; private set; }
    public Color DarkColor { get; private set; }
    public Color MenuColor { get; private set; }
    public Color LabelColor { get; private set; }
    public Color InfoColor { get; private set; }
    public Color ActiveColor { get; private set; }
}

这里dateJSON.toString()将字符串值存储到jsonData变量。 jsonData是JsonObject。

然后您尝试从字符串

中检索JsonArray
jsonData.put("jsonData", dateJSON.toString()); 

<强> EDITED

如果要从dateJson对象中检索数组 替换此行

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

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