如何将 jsonarray 存储到字符串数组 android



如何将jsonarray存储到arraylist string? 请任何帮助,非常感谢。 这是我的 JSON 阵列

{  
"ids":[  
{  
"noid":"2"
},
{  
"noid":"3"
}
],
"success":1
}

我想将身份"noid"的值存储在数组列表中。 这是我在安卓中的代码。

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";
}
?>

如果我检查数组是否为空,它总是返回 true。

使用 Gson 库。

List<String> list= new Gson().fromJson(idSet, new TypeToken<ArrayList<String>>(){}.getType());

最新更新