2016-07-09 40 views
-3

我使用API​​,並返回我一些數據,我的網址在JSON:我收到錯誤非法串偏移 '的requestId'

這是實際的URL數據:

data={%22requestId%22%3A%22546b384ce51f469a2e8b4567%22%2C%22numbers%22%3A{%22917566559950%22%3A{%22date%22%3A%222014-11-18+17%3A45%3A59%22%2C%22status%22%3A1%2C%22desc%22%3A%22DELIVERED%22}}} 

我的PHP代碼是插入數據的數據庫,但得到的錯誤:

$request = $_REQUEST["data"]; 
$jsonData = json_decode($request,true); 
$link = mysqli_connect("127.0.0.1", "root", "", "table"); 
foreach($jsonData as $key => $value) 
{ 
    $requestID = $value['requestId'] ; 
    $userId = $value['userId']; 
    $senderId = $value['senderId']; 
    foreach($value['report'] as $key1 => $value1) 
     { 
    //detail description of report 
    $desc = $value1['desc']; 
    // status of each number 
    $status = $value1['status']; 
    // destination number 
    $receiver = $value1['number']; 
    //delivery report time 
    $date = $value1['date']; 
    $query = "INSERT Query for store record "; 
    mysqli_query($link, $query); 
} 
} 

這裏有什麼問題的錯誤 警告:非法串關在線11設置'requestId' 警告:非法字符串偏移'userId'..... 請解決它....

回答

1

TLDR;

You should remove the foreach($jsonData as ...) around your code. 

你是這個關聯數組foreaching與它的鍵:

{ 
"requestId":"546b384ce51f469a2e8b4567", 
"numbers":{ 
    "917566559950":{ 
    "date":"2014-11-18 17:45:59", 
    "status":1, 
    "desc":"DELIVERED" 
    } 
} 
} 

第一次迭代的關鍵將是「的requestId」和值字段將是「546b384ce51f469a2e8b4567」。在第二輪你得到了「數字」和上面的數組。

$requestID = $jsonData['requestId']; 
var_dump($requestID); // the requestID 


// foreaching in the assoc array inside the 'numbers' 
foreach ($jsonData["numbers"] as $key => $value) { 
    // description 
    $desc = $value['desc']; 
    // status of each number 
    $status = $value['status']; 
    // date 
    $date = $value['date'];  

    var_dump($key); // the key with 917... 
} 
+0

讓我補充: 1)這將有助於你和潛在的受訪者你的問題,如果您的解碼URL編碼的JSON。 2)你得到的錯誤信息是告訴你,你試圖使用'requestID'作爲一個字符串的索引。因此,$值必須是一個字符串。這將是你檢查價值$價值的原因,以及爲什麼。 –