0

次のPHPコードを使用してjson文字列を返します。

$itemURL = 'http://***.***.***/search?tag=Football&affiliate_id=&max_results=3';
$response = file_get_contents($itemURL);//curl_exec($curlHandle);
echo $response;
$response = array($response);
echo $response[0];

次のようなjson文字列を取得します。

[{"ID": "123"、 "説明": "チャンピオンフットボールネイビーTシャツ"、 "HighPrice":16.99、 "LowPrice":16.99、 "SalePrice":null、 "OnSale":false、 "URL" :"http://www.mystore.com/itemDescription"、 "ImageURL": "http://www.mystore.com/mainstore/045.jpg"、 "LargeImageURL":"http://www.mystore。 com / mainstore / 045.jpg "、" ThumbnailImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" MiniImageURL ":" http://www.mystore.com/mainstore/045 jpg "、" AffiliateID ":" "}、{" ID ":" 23 "、"説明":"チャンピオンフットボールネイビーTシャツXL"、" HighPrice ":16.99、" LowPrice ":16.99、" SalePrice ":null、" OnSale ":false、" URL ":" http://www.mystore。 com / itemDescription "、" ImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" LargeImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" ThumbnailImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" MiniImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" AffiliateID ":" "} ]"SalePrice":null、 "OnSale":false、 "URL": "http://www.mystore.com/itemDescription"、 "ImageURL": "http://www.mystore.com/mainstore/045.jpg "、" LargeImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" ThumbnailImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" MiniImageURL ": "http://www.mystore.com/mainstore/045.jpg"、 "AffiliateID": ""}]"SalePrice":null、 "OnSale":false、 "URL": "http://www.mystore.com/itemDescription"、 "ImageURL": "http://www.mystore.com/mainstore/045.jpg "、" LargeImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" ThumbnailImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" MiniImageURL ": "http://www.mystore.com/mainstore/045.jpg"、 "AffiliateID": ""}]http://www.mystore.com/mainstore/045.jpg "、" ThumbnailImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" MiniImageURL ":" http://www。 mystore.com/mainstore/045.jpg "、" AffiliateID ":""}]http://www.mystore.com/mainstore/045.jpg "、" ThumbnailImageURL ":" http://www.mystore.com/mainstore/045.jpg "、" MiniImageURL ":" http://www。 mystore.com/mainstore/045.jpg "、" AffiliateID ":""}]

ただし、$ response [0]をエコーすると、文字列全体が取得されます。json_decodeまたはencodeを使用すると、文字列が取得されますが、引用符で囲まれています。このダングなものをキャストする方法がわからないので、オブジェクトの配列として動作し、それを反復処理できます。事前に助けてくれてありがとう

4

3 に答える 3

2

これを試して...

$itemURL = 'http://***.***.***/search?tag=Football&affiliate_id=&max_results=3';
$response = file_get_contents($itemURL);//curl_exec($curlHandle);
$response = json_decode($response);

次のようなオブジェクトが表示されます...

array(2) {
  [0]=>
  object(stdClass)#1 (12) {
    ["ID"]=>
    string(3) "123"
    ["Description"]=>
    string(30) "Champion Football Navy T-shirt"
    ["HighPrice"]=>
    float(16.99)
    ["LowPrice"]=>
    float(16.99)
    ["SalePrice"]=>
    NULL
    ["OnSale"]=>
    bool(false)
    ["URL"]=>
    string(38) "http://www.mystore.com/itemDescription"
    ["ImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["LargeImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["ThumbnailImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["MiniImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["AffiliateID"]=>
    string(0) ""
  }
  [1]=>
  object(stdClass)#2 (12) {
    ["ID"]=>
    string(2) "23"
    ["Description"]=>
    string(33) "Champion Football Navy T-shirt XL"
    ["HighPrice"]=>
    float(16.99)
    ["LowPrice"]=>
    float(16.99)
    ["SalePrice"]=>
    NULL
    ["OnSale"]=>
    bool(false)
    ["URL"]=>
    string(38) "http://www.mystore.com/itemDescription"
    ["ImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["LargeImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["ThumbnailImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["MiniImageURL"]=>
    string(40) "http://www.mystore.com/mainstore/045.jpg"
    ["AffiliateID"]=>
    string(0) ""
  }
}

次に、$ response [0]、$response[1]などを使用してJSONオブジェクトにアクセスできます。インスタンスに$response[0]->AffiliateIDなどのフィールドの特定の名前を使用します。

于 2012-06-29T22:05:25.073 に答える
1

あなたが探しているプロパティを呼び出す必要があります

IE:

echo response[0].ID; //getting the 1st item in the response array then access the "ID" property of the json object.

json応答は動的オブジェクトであり、オブジェクトを最初から作成したかのように、すべてのプロパティを操作できます。

[{...および...}]内の値はオブジェクト値です。

それで...

var json = '{"ID" : 1, "Prop1" : "Value1", Prop2 : "Value2" }';

jQueryのparseJSONメソッドを使用して解析できます

var obj= jQuery.parseJSON(json);
echo obj.ID; //1
echo obj.Prop1; //Value
echo obj.Prop2; //Value2

{と}の周りに[...と...]がある場合、このオブジェクトは配列であり、それに応じて処理する必要があることがわかります。

お役に立てれば。

wm

于 2012-06-29T22:07:00.057 に答える
0
if (mysql_num_rows($result) > 0) {
$response["events"] = array();

while ($row = mysql_fetch_array($result)) {
      $tmparr = array();
        $tmparr["uid"] = $row["uid"];
        $tmparr["date"] = $row["date"];
        $tmparr["hours"] = $row["hours"];
        $tmparr["store_name"] = $row["store_name"];
        $tmparr["event_information"] = $row["event_information"];
        $tmparr["event_type"] = $row["event_type"];
        $tmparr["Phone"] = $row["Phone"];
        $tmparr["price"] = $row["price"];
        $tmparr["address"] = $row["address"];
        $tmparr["image_url"] = $row["image_url"];
        $tmparr["created_at"] = $row["created_at"];
        $tmparr["updated_at"] = $row["updated_at"];
        array_push($response["events"], $tmparr);
}    
$response["success"] = 1;
echo json_encode($response);

あなたが何をしようとしているのか理解できませんが、これは、SQLクエリ($ result)からデータの行の配列を返すために使用する方法です。これがお役に立てば幸いです

于 2012-06-29T22:10:32.100 に答える