如何将jsonarray存储到arraylist字符串?请任何帮助,不胜感激。 这是我的jsonarray
{
"ids":[
{
"noid":"2"
},
{
"noid":"3"
}
],
"success":1
}
我想将标识“ noid”的值存储在arraylist中。 这是我在android中的代码。
protected String doInBackground(String... args) {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("qid", qsid));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jParser.makeHttpRequest(
main_url, "GET", params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt("success");
if (success == 1) {
// successfully received product details
JSONArray idSet = json
.getJSONArray("ids"); // JSON Array
for(int i = 0; i < idSet.length(); i++) {
JSONObject outputId = idSet.getJSONObject(i);
arrayList.add(outputId.getString("noid").toString());
}
}else{
Toast.makeText(getApplicationContext(), json.getString("result"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
这是我的php文件。
<?php
require_once 'mobile_question_query.php';
$j = array();
if(isset($_GET["qid"])){
$id = $_GET['qid'];
$fetchallqObject = new Questions();
if(!empty($id)){
$json_array = $fetchallqObject->fetchAllQuestionsID($id);
echo json_encode($json_array);
}else{
$j['result'] = "No id!";
echo json_encode($j);
}
}else{
echo "no id found";
}
?>
如果我检查数组是否为arraylist为空,则它始终返回true。
答案 0 :(得分:0)
使用Gson库。
<a class="nav-link" data-toggle="collapse" data-target=".navbar-collapse.show" href="#reviews">Reviews</a>