2015-10-19 236 views
-1
// Reading json file from assets folder 
     StringBuffer sb = new StringBuffer(); 
     BufferedReader br = null; 
     try { 
      br = new BufferedReader(new InputStreamReader(getAssets().open(
        "boysquestion.json"))); 
      String temp; 
      while ((temp = br.readLine()) != null) 
       sb.append(temp); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       br.close(); // stop reading 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     String myjsonstring = sb.toString(); 
     // Try to parse JSON 

     String question = null; 
     try { 
      // Creating JSONObject from String 
      JSONObject jsonObjMain = new JSONObject(myjsonstring); 

      // Creating JSONArray from JSONObject 
      JSONArray jsonArray = jsonObjMain.getJSONArray("category"); 

      // JSONArray has x JSONObject 
      for (int i = 0; i < jsonArray.length(); i++) { 

       // Creating JSONObject from JSONArray 
       JSONObject jsonObj = jsonArray.getJSONObject(i); 

       // Getting data from individual JSONObject 
       question = jsonObj.getString("question"); 
       int no_score = jsonObj.getInt("no_score"); 
       int yes_score = jsonObj.getInt("yes_score"); 
       int category = jsonObj.getInt("category"); 


       Log.d("question boys", question); 

       tvBoyGirl.setText(question); 
       Log.d("random", question); 

      } 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

JSON響應,如何提取所有的東西:解析JSON響應提取

[{"category":1,"no_score":3,"question":"Does he ever send the first text of a conversation?","yes_score":1},{"category":1,"no_score":3,"question":"Does he reply with two or three word answers?","yes_score":1}] 

以上是JSON響應..我想提取每everything..ie類別,no_score,yes_score和問題。 。

我曾嘗試

question = jsonObj.getString("question"); 

,但我沒有得到..任何人都可以幫我解壓。

+0

顯示你的整個的JSON代碼解析 –

+0

這不是一個JSONObject,它的JSONArray – warlock

+0

我已經更新了我的問題..可以告訴我如何提取所有信息 –

回答

1

這是我做的畢竟事情

ListQuestion(); 


     tvQuestionText.setText(questions.get(i)); 


     imUp.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       i++; 
       question_counter++; 

       tvQuestionCounter.setText(String.valueOf(question_counter)); 

       if (!questions.isEmpty()) { 

        tvQuestionText.setText(questions.get(i)); 

        if (yes_Score1.get(i) == -1) { 
         score = score - 1; 
        } else { 
         score = score + yes_Score1.get(i); 
        } 


        System.out.println(score); 
        Log.d("yes_per", String.valueOf(score)); 


        counter++; 
        if (counter == 25) { 

         calculation(); 
        } 
       } 
      } 
     }); 


     imDown.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       i++; 
       question_counter++; 
       tvQuestionCounter.setText(String.valueOf(question_counter)); 
       if (!questions.isEmpty()) { 

        tvQuestionText.setText(questions.get(i)); 

        if (no_Score1.get(i) == -1) { 
         score = score - 1; 
        } else { 
         score = score + no_Score1.get(i); 
        } 

        Log.d("no_per", String.valueOf(score)); 


        System.out.println(score); 

        counter++; 
        if (counter == 25) { 
         calculation(); 
        } 
       } 
      } 
     }); 
    } 
1

Json object開始於{json array開始於[。在你的情況下,json是一個數組。

所以讀取它作爲一個數組而不是jsonobject

即,您需要像下面一樣閱讀它。

JSONArray jsonArray = new JSONArray(myjsonstring);

並通過陣列迭代和讀取的每個值。你的for loop看起來不錯。

for (int i = 0; i < jsonArray.length(); i++) { 

      JSONObject jsonObj = jsonArray.getJSONObject(i); 
      question = jsonObj.getString("question"); 
      int no_score = jsonObj.getInt("no_score"); 
      int yes_score = jsonObj.getInt("yes_score"); 
      int category = jsonObj.getInt("category"); 

      tvBoyGirl.setText(question); 

     } 
-1

試試這個:

JSONArray jArray = new JSONArray(jsonString); 
for(int i=0;i<jArray.length();i++) 
{ 
    JSONObject jObj = jArray.getJSONObject(i); 

    question = jObj.getString("question"); 
    int no_score = jObj.getInt("no_score"); 
    int yes_score = jObj.getInt("yes_score"); 
    int category = jObj.getInt("category"); 
} 
+0

我想從響應中獲得具體的價值..我如何得到具體的價值。 ? –