2012-11-22 224 views
1

首先感謝花時間看看我的問題,我是android開發新手,我遇到了一個來自PHP服務器的數組來自JSON編碼的問題,我試着去看看對於答案,但JSON的所有示例使用字段名稱來解碼數組,我的字段名稱不存在。json數組遇到問題

這是陣列,可以是許多串的一個例子:

[["104733","\u5e38\u4e16\u7530 \u9752","images\/myphoto\/104733_20120711101030_sq_s.jpg","062556","2012\/11\/21","test20121121-2","Test2\n\u643a\u5e2f\u304b\u3089","11.11","333.33","555.55","77.77","22:22:22","20:44:44","19:07:06","17:29:28",0," ","0"," "," ","61116",0," "] 
,["104733","\u5e38\u4e16\u7530 \u9752","images\/myphoto\/104733_20120711101030_sq_s.jpg","062555","2012\/11\/21","test20121121","Test\n\u643a\u5e2f\u304b\u3089","11.11","333.33","555.55","77.77","22:22:22","20:44:44","19:07:06","17:29:28",0," ","0"," "," ","61117",0," "]] 

基本上其中說\ u643a \ u5e2f \ u304b \ u3089是日語字符攜帯から。

我設法使用這種循環手動分離字段,但仍然以編碼格式獲取名稱。

     for(int i=0; i<num; i++){ 
      //Separate the string in to the different groups 
      endIdx = strcline.indexOf("]", startIdx)+1; 
      String temp = strcline.substring(startIdx, endIdx); 

      //Get the user id from the string temp 
      startIdx2 = 2; 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String userId = temp.substring(startIdx2, endIdx2); 
      Log.v("temp", userId); 
      startIdx2 = endIdx2 + 3; 

      //Get the user name 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String userName = temp.substring(startIdx2, endIdx2); 
      Log.w("temp", userName); 
      //String userName2 = new String(userName.getBytes(), "UTF-8"); 

      //Log.d("fullurl", userName2); 
      //Log.w("temp", String.valueOf(userName2)); 
      startIdx2 = endIdx2 + 3; 

      //Get the user photo url 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String userPhoto = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", userPhoto); 
      startIdx2 = endIdx2 + 3; 

      //Get the training id 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String trainingId = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", trainingId); 
      startIdx2 = endIdx2 + 3; 

      //Get the diary's date 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String diaryDate = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", diaryDate); 
      startIdx2 = endIdx2 + 3; 

      //Get the Diary's title 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String diaryTitle = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", diaryTitle); 
      startIdx2 = endIdx2 + 3; 

      //Get the diary's content 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String diaryContent = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", diaryContent); 
      startIdx2 = endIdx2 + 3; 

      //Get the swim distance 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String swimDistance = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", swimDistance); 
      startIdx2 = endIdx2 + 3; 

      //Get the bike distance 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String bikeDistance = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", bikeDistance); 
      startIdx2 = endIdx2 + 3; 

      //Get the run distance 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String runDistance = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", runDistance); 
      startIdx2 = endIdx2 + 3; 

      //Get the other distance 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String otherDistance = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", otherDistance); 
      startIdx2 = endIdx2 + 3; 

      //Get the swim time 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String swimTime = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", swimTime); 
      startIdx2 = endIdx2 + 3; 

      //Get the bike time 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String bikeTime = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", bikeTime); 
      startIdx2 = endIdx2 + 3; 

      //Get the run time 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String runTime = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", runTime); 
      startIdx2 = endIdx2 + 3; 

      //Get the other time 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String otherTime = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", otherTime); 
      startIdx2 = endIdx2 + 2; 

      //Get the number of photos 
      endIdx2 = temp.indexOf(",", startIdx2); 
      String numPhotos = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", numPhotos); 
      startIdx2 = endIdx2 + 2; 

      //Get the string for the Photos 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String stringPhotos = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", stringPhotos); 
      startIdx2 = endIdx2 + 3; 

      //Get the like flag 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String likeFlg = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", likeFlg); 
      startIdx2 = endIdx2 + 3; 

      //Get the name of friend who liked 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String friendLiked = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", friendLiked); 
      startIdx2 = endIdx2 + 3; 

      //Get the string for the likes 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String stringLike = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", stringLike); 
      startIdx2 = endIdx2 + 3; 

      //Get the id for this array 
      endIdx2 = temp.indexOf(",", startIdx2) - 1; 
      String arrayId = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", arrayId); 
      startIdx2 = endIdx2 + 2; 

      //Get the number of comments 
      endIdx2 = temp.indexOf(",", startIdx2); 
      String numberComments = temp.substring(startIdx2, endIdx2); 
      Log.d("temp", numberComments); 
      startIdx2 = endIdx2 + 2; 

      //Get the string for the comments 
      endIdx2 = temp.indexOf("]", startIdx2) - 1; 
      String stringComments = temp.substring(startIdx2, endIdx2); 
      Log.e("temp", stringComments); 

      startIdx = endIdx + 1; 

      arrayTemp[i] = temp; 
      Log.w("LogStr", temp); 
     } 
+2

爲什麼不使用庫來解析json,如jackson,Gson等? –

+0

我想弄清楚如何使用gson,但它說它需要名稱字段。 – David

回答

0

你的json是一個字符串數組的數組,這就是爲什麼你沒有字段名。它們只存在於物體中。

用String操作解析json是件痛苦的事情。你應該看看org.json包中的類,並在這裏開始解析JSONArray

JSONArray a1 = new JSONArray(json); 
for (int i = 0; i < a1.length(); i++) { 
    JSONArray a2 = a1.getJSONArray(i); 
    for (int j = 0; j < a2.length(); j++) { 
    String s1 = a2.getString(j); 
     // ... 
    } 
} 
+0

我明白你的意思是數組是字符串數組,但是正如我之前提到的,對於android和java開發我還是比較新的,可能會更明確些。 – David

+0

@David我用一個例子更新了我的答案 – brillenheini

+0

謝謝@brillenheini,我會試一試並讓你知道。 – David