如何在android volley

时间:2016-04-06 18:15:17

标签: android json

这是我需要解析的输出。 usertype_name的值应存储在String数组中。请帮帮我。

{
    0: {
    usertype_name: "Member-Individual"
    },
    1: {
    usertype_name: "Member-Institutional"
    },
    2: {
    usertype_name: "Officer-Member"
    },
    3: {
    usertype_name: "Officer-Admin"
    },
    4: {
    usertype_name: "Officer-President"
    },
    current_position: "Member-Individual"
}

在凌空分析JSON时,通常我只是这样做:

public void onResponse(String s) {
    JSONArray array= new JSONArray(s.toString());
    for(int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);
        String text = object.getString("usertype_name");
    }
}

但是由于结果上显示了增量数字,这对我来说很奇怪,令人困惑。我也发布我的网络服务代码。它是这样的,usertype_name是查询的结果,而current_position是另一个查询的结果。如果我删除current_position,则数字将会消失。

$result = mysql_result(mysql_query($query),0);
$result2 = mysql_query($query2);

while($line = mysql_fetch_array($result2, MYSQL_ASSOC)){
    $results[] = $line;
    }
$results['current_position'] = $result;

echo (json_encode($results));

1 个答案:

答案 0 :(得分:0)

像这样更改你的json字符串,

{
  "array": [
    "Member-Individual",
    "Member-Institutional",
    "Officer-Member"
  ],
  "current_position": "6"
}

和php代码示例:

 <?php
    class User {
        public $array = array("Member-Individual", "Member-Institutional", "Officer-Member");
        public $current_position = "6";

        public function __construct() {

        }
    }



    $user = new User();

    echo json_encode($user);
?>