簡體   English   中英

JSONArray無法轉換為JSONObject異常

[英]JSONArray cannot be converted to JSONObject exception

我有一個按以下方式構造的JSON字符串,它引發了一個異常,將其傳遞給JSONArray timeJSONArray = new JSONArray(time);

這是類型為0的錯誤Value [{"daysByte":158,"from":1020,"to":1260},{"daysByte":96,"from":1020,"to":1320}] at 0 of type org.json.JSONArray cannot be converted to JSONObject這是我接收數組的方式,無法更改它,因此在將其轉換為JSON對象而不是當前格式的JSON String時遇到麻煩。我究竟做錯了什么?

[   
   [  
      {  
         "daysByte":30,
         "from":660,
         "to":1290
      },
      {  
         "daysByte":96,
         "from":660,
         "to":1320
      },
      {  
         "daysByte":128,
         "from":1050,
         "to":1290
      }
   ],
   [  
      {  
         "daysByte":252,
         "from":690,
         "to":840
      },
      {  
         "daysByte":252,
         "from":1050,
         "to":1260
      }
   ]
]

這是我正在使用的代碼。 我正在將值作為字符串傳遞

public ArrayList<String> getTimeList(String time){

        System.out.println("PLACES ACTIVITY " + time);
        ArrayList<String> times = new ArrayList<>();
        try{
            //JSONObject timeJSONObject = new JSONObject(time);
            JSONArray timeJSONArray = new JSONArray(time);
            ArrayList<LegacyTimeSpan> timeSpanList = new ArrayList<>();

            LegacyTimeSpanConverterImpl converter = new LegacyTimeSpanConverterImpl();


            for(int i = 0; i < timeJSONArray.length(); i++){

                int daysByte = timeJSONArray.getJSONObject(i).getInt("daysByte");
                int from = timeJSONArray.getJSONObject(i).getInt("from");
                int to = timeJSONArray.getJSONObject(i).getInt("to");

                System.out.println("TO " + to);

                LegacyTimeSpan timeSpan = new LegacyTimeSpan(daysByte, from, to);
                timeSpanList.add(timeSpan);

            }

            Log.d("Time span list", timeSpanList.toString());
            WeekSpan weekSpan = converter.convertToWeekSpan(timeSpanList);
            List<DayTimeSpanPair> dayTimeSpanPair = weekSpan.toDayTimeSpanPairs();
            for(int i = 0; i< dayTimeSpanPair.size(); i++){

                String timeRange = buildTimeString(dayTimeSpanPair.get(i));
                times.add(timeRange);


            }
        } catch(JSONException e){
            Log.d("PLACES EXCEPTION JSON",e.getMessage());
        }

        return times;
    }

我認為當您聲明json格式時,此代碼應該可以工作。

             [
                [
                  {
                  } ,{},{}             // Json Object Structure as u defined in you Question
topArray =      ],
                [  
                  {
                  },{},{}
                ]
             ]


for(JSONArray objArray : topArray){
    for(JSONObject eachObject : objArray){
   System.out.println(eachObject.get("daysByte"););
   System.out.println(eachObject.get("from");
   System.out.println(eachObject.get("to");
    }
}

您好以下代碼適用於您嘗試過的json。 它是針對您的json而不是通用的。 因此,如果您願意,可以使用它。

try{
    JSONArray jsonArray = new JSONArray(data); //insted "Data" pass your json Strint
       for(int i=0 ; i<jsonArray.length() ; i++){
           JSONArray internalArray = jsonArray.getJSONArray(i);
           for(int j = 0 ; j < internalArray.length() ; j++){
               JSONObject internalObject = internalArray.getJSONObject(j);
               Log.d("data" ,  internalObject.getString("daysByte"));
               Log.d("data" ,  internalObject.getString("from"));
               Log.d("data" ,  internalObject.getString("to"));
           }
       }

   }catch(Exception e){
       Log.d("data" ,"Error");
   }
}

您有兩個數組,另一個數組。

您必須這樣做:

for(JSONArray temp: timeJsonArray)
{
   // try to convert to json object
}

基本上有兩個JSON數組,但是您只訪問一個數組,這就是為什么顯示此錯誤的原因

   try {
        JSONArray jsonArray = new JSONArray(a);

        for (int j=0;j<jsonArray.length();j++) {

            JSONArray timeJSONArray = jsonArray.getJSONArray(j);

            for(int i = 0; i < timeJSONArray.length(); i++){

                int daysByte = timeJSONArray.getJSONObject(i).getInt("daysByte");
                int from = timeJSONArray.getJSONObject(i).getInt("from");
                int to = timeJSONArray.getJSONObject(i).getInt("to");

                System.out.println("TO " + to);


            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

它是一個二維數組。

System.out.println("days");
String content = new Scanner(new File("C:/day.txt")).useDelimiter("\\Z").next();
Day[][] customDayWrap = new Gson().fromJson(content, Day[][].class);
    for (Day[] days : customDayWrap) {
        for (Day day : days) {
            System.out.println(day.getDaysByte());
            System.out.println(day.getFrom());
            System.out.println(day.getTo());
        }
    }

您的日間班將是這樣。

public class Day {

@SerializedName("daysByte")
@Expose
private Integer daysByte;
@SerializedName("from")
@Expose
private Integer from;
@SerializedName("to")
@Expose
private Integer to;

/**
 * 
 * @return
 *     The daysByte
 */
public Integer getDaysByte() {
    return daysByte;
}

/**
 * 
 * @param daysByte
 *     The daysByte
 */
public void setDaysByte(Integer daysByte) {
    this.daysByte = daysByte;
}

/**
 * 
 * @return
 *     The from
 */
public Integer getFrom() {
    return from;
}

/**
 * 
 * @param from
 *     The from
 */
public void setFrom(Integer from) {
    this.from = from;
}

/**
 * 
 * @return
 *     The to
 */
public Integer getTo() {
    return to;
}

/**
 * 
 * @param to
 *     The to
 */
public void setTo(Integer to) {
    this.to = to;
}

}

我對此進行了測試(我正在使用Google GSON庫),並且能夠成功讀取它。

在調試了您的Json陣列1個小時之后,我終於設法弄清了您的實際問題。 它不僅是一個Json Array,而且它的數組位於數組內部。

所以像這樣循環

 for (int i = 0; i < timeJSONArray.length(); i++) {

            for(int j= 0;j<i;j++) {
                int daysByte = timeJSONArray.getJSONArray(i).getJSONObject(j).getInt("daysByte");
                int from = timeJSONArray.getJSONArray(i).getJSONObject(j).getInt("from");
                int to = timeJSONArray.getJSONArray(i).getJSONObject(j).getInt("to");
                Log.d("dataRecieved", "daybyte " + daysByte + "from " + from + "to " + to);
            }
}

並根據需要進行其他操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM