2016-02-26 135 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); 
      } 
} 

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

相關問題