0

我使用此方法生成通行证:

公共函数 setJSON($JSON) {
    如果(json_decode($JSON)!== 假){
        $this->JSON = $JSON;
        返回真;
    }
    $this->sError = '这不是 JSON 字符串。';
    返回假;
    }
我调用这个方法来生成传递:
$pass->setJSON('{
    "passTypeIdentifier": "'.$passTypeID.'",
    “格式版本”:1,
    .....................
“条码”: {
        "altText" : "'.$alt.'",
        "格式" : "PKBarcodeFormatPDF417",
        "message": "会员卡",
        "messageEncoding": "iso-8859-1",
        "changeMes​​sage" : "这个 pass 现在有 altText %@ !"
        },
“地点”:[
    {
      “经度”:104.89529371261597,
      “纬度”:11.576150037278605,
      "relevantText": "CamMob (dis. 1%)"
    },
     .....................
]
    }');
但是现在我不会静态地编写这些位置,而是通过以下方式从数据库中获取这些数据:
$query5 = mysql_query("select * from company");
    而 ($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 的问题吗?如果我这样做,我怎样才能将这 2 个 json 组合在一起?

4

1 回答 1

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));
于 2013-05-10T03:45:49.397 回答