Php 如何从2个不同的json_编码生成过程
我使用此方法生成一个pass:Php 如何从2个不同的json_编码生成过程,php,json,passbook,Php,Json,Passbook,我使用此方法生成一个pass:公共函数setJSON($JSON){ public function setJSON($JSON) { if(json_decode($JSON) !== false) { $this->JSON = $JSON; return true; } $this->sError = 'This is not a JSON string.'; return false; } if(json_decod
公共函数setJSON($JSON){ public function setJSON($JSON) { if(json_decode($JSON) !== false) { $this->JSON = $JSON; return true; } $this->sError = 'This is not a JSON string.'; return false; } if(json_decode($json)!==false){ $this->JSON=$JSON; 返回true; } $this->sError='这不是JSON字符串'; 返回false; } 我调用此方法来生成pass-by:
$pass->setJSON('{ $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%)" }, ..................... ] }'); “passTypeIdentifier”:“'.$passTypeID.”, “格式版本”:1, .............. “条形码”:{ “altText”:“'.$alt.”, “格式”:“PKBarcodeFormatPDF417”, “信息”:“会员卡”, “消息编码”:“iso-8859-1”, “changeMessage”:“此通行证现在有altText%@!” }, “地点”:[ { “经度”:104.89529371261597, “纬度”:11.5761500372605, “相关文本”:“CamMob(dis.1%)” }, ..................... ] }'); 但现在我不再静态地编写这些位置,而是通过以下方式从数据库中获取这些数据:$query5=mysql_query(“select*from company”); $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>
while($row5=mysql\u fetch\u数组($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);
错误日志(“位置:.$jsonString,0);
$pass->setJSON($jsonString);
$pass->setJSON($pass){
“passTypeIdentifier”:“'.$passTypeID.”,
“条形码”:{
“altText”:“'.$alt.”,
“格式”:“PKBarcodeFormatPDF417”,
“信息”:“会员卡”,
“消息编码”:“iso-8859-1”,
“changeMessage”:“此通行证现在有altText%@!”
}
}');
当我测试更新pass时没有错误,但是我不能像以前一样在锁屏上看到pass。因此,这意味着我尚未将这些位置包括在通行证中。我应该换什么?这是setJSON方法的问题吗?如果我这样做,我如何将这两个JSON组合在一起?与其直接构造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));
你们有多少个地点?记住,只有前10个会显示。此外,如果要验证是否将位置添加到JSON,可以将.pkpass文件的扩展名更改为.zip以查看代码生成的内容,或者将JSON写入php日志。如果从数据库中选择位置,如何通过编码将它们添加到已创建的过程中?我不知道如何组合不同的encode_json!我尝试这样做:………“位置”:“$jsonString”。}”);然后我得到这个错误:[2013年5月10日04:25:52欧洲/柏林]存折错误日志:[2013-05-10 09:25:54+0700]获取通行证任务(通行证类型pass.cam-mob.passbookpasstest,序列号7,如果自2013年5月10日星期五09:14:33 GMT+7修改;带有web服务url)遇到错误:接收到无效的通行证数据(由于密码无效,无法读取密码。)谢谢,我也只是用这种方式进行测试,但我没有将所有内容都写入数组,而是使用decode,但我也遇到了一些错误!$dataArray=json_decode($data,true);$result=array_merge($dataArray,$location);$String=json_encode($result);error_log(“所有数据:”.$String,0);[2013年5月10日10:55:32亚洲/金边]所有数据:{“passTypeIdentifier”:“pass.cam mob.passbookpasstest”,“formatVersion”:1,“serialNumber”:“7”,“teamIdentifier”:“3J29YP53Q5”,“authenticationToken”:“Fy7Iiew81JNdsdaG”,“webService url”:“http:\/\/192.168.1.202\/passesWebserver\/webService\/”,“associatedStoreIdentifiers”[485596906],错误:[2013年5月10日05:55:32欧洲/柏林]存折错误日志:[2013-05-10 10:55:33+0700]获取通行证任务(通行证类型pass.cam-mob.passbookpasstest,序列号7,如果自2013年5月10日星期五10:10:34 GMT+7修改;使用web服务url)遇到错误:收到无效的通行证数据(通行证无效,无法读取。)只需使用上面修改过的代码。编写JSON,解码然后重新编码是非常低效的。