【问题标题】:JSONArray cannot be converted to JSONObject exceptionJSONArray 无法转换为 JSONObject 异常
【发布时间】:2016-02-26 06:27:27
【问题描述】:

我有一个按以下方式构造的 JSON 字符串,它会抛出一个异常并将其传递给 JSONArray timeJSONArray = new JSONArray(time);

这是错误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 字符串,这是它当前的格式。我做错了什么?

[   
   [  
      {  
         "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,还要显示代码。始终显示代码。
  • 查看我对上述问题的回答,基本上有两个数组在另一个数组中,因此您必须考虑数组,目前您正在尝试将第一个数组放入不正确的 JSONObject
  • timeJSONArray.getJSONObject(i)? i 没有 JsonObject,你有一个数组

标签: java json


【解决方案1】:

当您声明 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");
    }
}

【讨论】:

    【解决方案2】:

    您好,以下代码适用于我尝试过的您的 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");
       }
    }
    

    【讨论】:

    • 如果你觉得有用,请标记为答案
    • 它不会超过第一行。这就是它引发异常的地方,因此在它之后不会执行任何代码。
    • 我没有收到您的评论,请您再次解释一下您的问题
    • JSONArray jsonArray = new JSONArray(data) 行抛出异常,因此它下面的任何内容都不会执行。它会直接跳到“catch”
    • 我已经使用你给出的相同 json 进行了测试,它正在工作,你可能会得到不同的 json
    【解决方案3】:

    你有两个数组,一个数组在另一个数组中。

    你必须这样做:

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

    【讨论】:

    • 如果在JSONArray timeJSONArray = new JSONArray(time);这一行抛出错误,那么我将如何在它之后创建一个for循环?
    • stackoverflow.com/a/2414332/5188230 看到这个,它是一个二维 JSON 数组
    【解决方案4】:

    这是一个二维数组。

    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 库),并且能够成功读取它。

    【讨论】:

      【解决方案5】:

      基本上,有两个 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();
          }
      

      【讨论】:

      • 只有一个数组。请检查一下。
      • @chandankumarkushwaha 它的两个数组看到这个:stackoverflow.com/a/2414332/5188230
      • 但总的来说它被添加到一个数组中。请检查
      • 嘿伙计,转到此链接并将您的 json 粘贴到那里,首先了解结构,然后您将能够解决它json.parser.online.fr
      【解决方案6】:

      在调试您的 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);
                  }
      }
      

      并根据需要做其他事情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多