我使用此方法生成传递:
public function setJSON($JSON) { if(json_decode($JSON) !== false) { $this->JSON = $JSON; return true; } $this->sError = 'This is not a JSON string.'; return false; }我将此方法称为生成传递:
$pass->setJSON('{ "passTypeIdentifier": "'.$passTypeID.'", "formatVersion": 1, .............. "barcode": { "altText" : "'.$alt.'", "format" : "PKBarcodeFormatPDF417", "message": "Member-Card", "messageEncoding": "iso-8859-1", "changeMessage" : "This pass now has altText %@ !" }, "locations" : [ { "longitude" : 104.89529371261597, "latitude" : 11.576150037278605, "relevantText": "CamMob (dis. 1%)" }, ..................... ] }');但是现在我没有静态地写这些位置,我通过以下方式从数据库中获取这些数据:
$query5 = mysql_query("select * from company");
while ($row5 = mysql_fetch_array($query5)){
$companyName = $row5['relevantTextName'];
$discount = $row5['relevantTextDiscount'];
$long = $row5['longitute'];
$lat = $row5['latitute'];
$link = $row5['link'];
$location['locations'][] = array("longitute" => $long, "latitute" => $lat, "relevantText" => $companyName." (" . $discount. "%)" );
}
$jsonString = json_encode($location) ;
error_log("Locations: ".$jsonString,0);
$pass->setJSON($jsonString);
$pass->setJSON('{
"passTypeIdentifier": "'.$passTypeID.'",
"barcode": {
"altText" : "'.$alt.'",
"format" : "PKBarcodeFormatPDF417",
"message": "Member-Card",
"messageEncoding": "iso-8859-1",
"changeMessage" : "This pass now has altText %@ !"
}
}');</pre>
当我测试更新传递时没有错误,但是我无法像以前那样在锁定屏幕上看到传递。因此,这意味着我尚未将这些位置包含在通行证中。我应该改变什么?这是方法setJSON的问题吗?如果我喜欢这样,我怎样才能将这两个jsons组合在一起?
答案 0 :(得分:1)
不是直接构造JSON,而是先构造一个数组,然后将其转换为JSON。
$pass_data = array("passTypeIdentifier" => $passTypeID,
"formatVersion" => 1,
"barcode" => array (
"altText" => $alt,
"format" => "PKBarcodeFormatPDF417",
"message" => "Member-Card",
"messageEncoding" => "utf-8", // use UTF8 in case you want to encode Khmer or other non ASCII
"changeMessage" => "This pass now has altText %@ !"
),
"locations" => $locationsArray,
"organizationName" => "Digi club card",
"description" => "Membership card",
"logoText" => "Digiclub",
"foregroundColor" => "rgb(0,0,0)",
"backgroundColor" => "rgb(211,211,211)",
"generic" => array (
"headerFields" => array(
array ( "key" => "Status",
"label" => " ",
"value" => "Membership card"
),
),
"primaryFields" => array(
array ( "key" => "Name",
"value" => $memberName,
),
),
"secondaryFields" => array(
// Secondary Field data
),
"auxiliaryFields" => array(
// Auxiliary Field data
),
"backFields" => array(
// Backfiels Field data
),
)
);
然后将数组转换为JSON:
$pass->setJSON(json_encode($pass_data));