繁体   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