Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSONArray无法转换为JSONObject异常_Java_Json - Fatal编程技术网

Java JSONArray无法转换为JSONObject异常

Java JSONArray无法转换为JSONObject异常,java,json,Java,Json,我用以下方式构造了一个JSON字符串,它抛出一个异常,将其传递到JSONArray timeJSONArray=newjsonarray(time) 这是0处org.json.JSONArray类型的错误值[{“daysByte”:158,“from”:1020,“to”:1260},{“daysByte”:96,“from”:1020,“to”:1320}],无法转换为JSONObject这是我接收数组的方式,我无法更改它,因此,我很难将它转换为JSON对象,而不是JSON字符串(它当前的格式

我用以下方式构造了一个JSON字符串,它抛出一个异常,将其传递到
JSONArray timeJSONArray=newjsonarray(time)

这是0处org.json.JSONArray类型的错误
值[{“daysByte”:158,“from”:1020,“to”:1260},{“daysByte”:96,“from”:1020,“to”:1320}],无法转换为JSONObject
这是我接收数组的方式,我无法更改它,因此,我很难将它转换为JSON对象,而不是JSON字符串(它当前的格式)。我做错了什么

[   
   [  
      {  
         "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;
    }
public ArrayList getTimeList(字符串时间){
System.out.println(“场所活动”+时间);
ArrayList时间=新建ArrayList();
试一试{
//JSONObject timeJSONObject=新JSONObject(时间);
JSONArray timeJSONArray=新JSONArray(时间);
ArrayList timeSpanList=新建ArrayList();
LegacyTimeSpanConverterImpl转换器=新的LegacyTimeSpanConverterImpl();
for(int i=0;i
您有两个阵列,一个阵列在另一个阵列中

你必须这样做:

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();
    }
试试看{
JSONArray JSONArray=新JSONArray(a);
对于(int j=0;j
我认为这段代码应该可以在声明json格式时使用

您好,下面的代码适用于您的json,我已经尝试过了。它是针对您的json的特定代码,而不是泛型代码。因此,如果您需要,可以使用它

试试看{
JSONArray JSONArray=新JSONArray(数据);//替换的“数据”传递json命令
对于(int i=0;i它是一个2D数组

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数组,而且是数组中的数组

所以像这样循环

 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);
            }
}
for(int i=0;i对于(int j=0;jj不只是显示JSON,也显示代码)。请看我对上述问题的答案,基本上有两个数组,其中一个在另一个数组中,因此您必须考虑数组,当前您正试图将第一个数组放置到一个不正确的JSONORT对象<代码> TimeJSORAMARET.GETJSONObjor(i)中。
?如果在
i
中没有JsonObject,那么如果在这一行抛出错误,则有一个数组
JSONArray timeJSONArray=new JSONArray(time)
然后我如何才能在它之后创建for循环?看这个,它是一个二维JSON数组。它只是一个数组。请检查它。@chandankumarkushwaha它是一个两数组。看这个:但总的来说,它是在一个数组中添加的。请检查这个链接并粘贴你的JSON。首先了解结构,然后你会如果您发现它有用,那么请将它标记为answerIt's not pass the first line.这就是它引发异常的地方,因此在它之后没有代码执行。我没有收到您的评论,请您再次解释您的问题。异常在
JSONArray JSONArray=new JSONArray(数据)行引发
因此,下面的任何内容都不会执行。它将直接跳转到我使用您提供的相同json测试的“catch”,它正在工作,您可能会得到不同的json
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;
}
 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);
            }
}