// 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");
,但我沒有得到..任何人都可以幫我解壓。
顯示你的整個的JSON代碼解析 –
這不是一個JSONObject,它的JSONArray – warlock
我已經更新了我的問題..可以告訴我如何提取所有信息 –