如何从Android到PHP访问json子对象?

时间:2012-11-15 23:53:03

标签: php android json

我在Android中有这个结构:

HttpPost post = new HttpPost(url);

        mjson.put("receiveOrderId", receiveOrderId);
        mjson.put("packageId", packageId);
        mjson.put("partNumber", partNumber);
        mjson.put("qty", qty);
        mjson.put("serial", serial);
        mjson.put("locationId", locationId);

        json.put("version", "6");
        json.put("token", token);
        json.put("data", mjson);

        post.setHeader("json", json.toString());
        StringEntity se = new StringEntity(json.toString());
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));
        post.setEntity(se);
        response = client.execute(post);

我需要在php中解码,如何访问“partNumber”或“qty”的值?

以这种方式尝试,但它不起作用:

$data = file_get_contents('php://input');
$json = json_decode($data);
$version = $json->{'version'};
$token = $json->{'token'};
$receiveOrderId = $json->{'receiveOrderId'};
$packageId = $json->{'packageId'};
$partNumber = $json->{'partNumber'};
$qty = $json->{'qty'};
$serial = $json->{'serial'};
$locationId = $json->{'locationId'};

* 对不起我的英语,我来自墨西哥。

1 个答案:

答案 0 :(得分:0)

你可以打印$ json吗?

var_dump($json);

它看起来像:

'{"qty": 123, "version": "xxxxx"}';

确保索引名称(数量,版本,...)被双引号所包含,而不是单引号