2016-02-26 132 views
1

我有一个JSON字符串以下面的方式构造并抛出异常它穿入JSONArray timeJSONArray = new JSONArray(time);JSONArray不能转换到的JSONObject例外

这是错误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; 
    } 
+2

不要只显示JSON,显示代码。始终显示代码。 –

+0

见我的回答上述问题,基本上有两个数组中的其他一个,所以你必须要考虑的阵列,目前你正试图把第一阵列的JSONObject的这是不正确 –

+0

'timeJSONArray.getJSONObject(我)'?你没有一个JSONObject在'i',你有一个数组 –

回答

0

您有两个数组,其中一个数组。

你必须这样做:

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

如果错误在该行'JSONArray timeJSONArray =新JSONArray(时间)投掷;'那我怎么就能之后做一个for循环? – Rafa

+0

@ ralphie9224你试图打印时间的价值? –

+0

是的,这是问题的JSON格式。 – Rafa

0

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

我该如何解决它? – Rafa

+0

它只是一个单一的数组。请检查一下。 –

+0

@chandankumarkushwaha它的两个数组看看这个:http://stackoverflow.com/a/2414332/5188230 –

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"); 
    } 
} 
1

嗨下面的代码正在为您的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"); 
    } 
} 
+0

如果你觉得它有用,那么请将它标记为答案 –

+0

它不会超过第一行。这就是抛出异常的地方,所以在它之后没有执行代码。 – Rafa

+0

我没有得到你的评论,请你再解释一下你的问题 –

0

这是一个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()); 
     } 
    } 

而你的Day类将是这样的。

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

}

我测试了这(我使用谷歌GSON库),我是能够成功地读取它。

0

1小时调试JSON数组后,我终于弄清楚您的实际问题。它不仅是一个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); 
      } 
} 

而且,因为你需要做其他。

相关问题