douzhuo6270 2017-10-13 10:46
浏览 35

如何从PHP的javascript中获取JSON编码数组的属性?

I can't for the life of me get the values out from a JSON encoded array in javascript from PHP?

$company = new stdClass();
$company->PostCode   = $orgs->Organisation[$i]->BookingCategory->BookingDetails->OrganisationPostCode;
$company->Name       = $orgs->Organisation[$i]->OrganisationName;
$company->Address1   = $orgs->Organisation[$i]->BookingCategory->BookingDetails->OrganisationAddress1;
$company->Address2   = $orgs->Organisation[$i]->BookingCategory->BookingDetails->OrganisationAddress2;

array_push($myArr,$company);
$someJSON = json_encode($myArr);
echo $someJSON;

That gives me this which is returned

[{"PostCode":{"0":"mypostcode"},"Name":{"0":"mycomanyname"},"Address1":{"0":"myaddress1"},"Address2":{"0":"myaddress2"}}]

I've tried this and other permutations in xmlhttprequest:

var JSONObject = JSON.parse(this.responseText);
for (var key in JSONObject) {
    postcode = JSONObject[key][0]["PostCode"].PostCode
}

Some code omitted such as the loop etc. Any ideas?

  • 写回答

2条回答 默认 最新

  • dtmu88600 2017-10-13 10:56
    关注

    considering your code you have to do

    var JSONObject = JSON.parse(this.responseText);
    for (var key in JSONObject) {
      postcode = JSONObject[key].PostCode[0];
    }
    
    评论

报告相同问题?