我从webservice(soap xml)收到一个PHP数组。 从这个数组中,我想保留几个标签,然后将其转换为json(参见底部的示例)。
请参阅我从webservice收到的数组样本,如下所示:
(***标志着我在最后的Json中需要的信息)
Array (
[0] => stdClass Object (
***[Id] => 445946
[SystemName] =>
[TrackingPixelUrl] => http://www.pix.se/picture.png
[UrlName] => Movie_Renoir_445946
***[Name] => Movie: Film
[ForeignProductId] =>
[PackageId] =>
[OrganizationId] => 1045
[Version] => 12
[TemplateId] => 81
***[Image] => stdClass Object (
[MediaId] => 41315620
[MediaType] => Image
[Url] => http://images.pix.se
[Width] => 214
[Height] => 317
[ProducedBy] =>
[CopyrightBy] =>
[ImageType] => Image
)
[Status] => Active
[PublishedDate] => 2014-11-03T10:23:09.617
[RevisionDate] =>
[ExpirationDate] => 2014-11-14T00:00:00
[Relevance] => Regional
[ParentProductId] =>
[ProductType] => Product
[IsHighlight] =>
[Attributes] => stdClass Object (
[AttributeData] => Array (
[0] => stdClass Object (
[Id] => 18895726
[AttributeId] => 125
[MetaType] => String
[Value] => stdClass Object (
***[Data] => http://www.Movie.se
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 1
[Language] => 1
)
[1] => stdClass Object (
[Id] => 18895762
[AttributeId] => 102
[MetaType] => String
[Value] => stdClass Object (
***[Data] => Text text info tinfo text Long text
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 2
[Language] => 1
)
[2] => stdClass Object (
[Id] => 20562011
[AttributeId] => 106
[MetaType] => String
[Value] => stdClass Object (
***[Data] => 70 SEK
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 3
[Language] => 1
)
[3] => stdClass Object (
[Id] => 27970127
[AttributeId] => 99
[MetaType] => String
[Value] => stdClass Object (
[Data] => Movie: Film
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 11
[Language] => 1
)
[4] => stdClass Object (
[Id] => 27970128
[AttributeId] => 101
[MetaType] => String
[Value] => stdClass Object (
***[Data] => Short info text text text
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 11
[Language] => 1
)
[5] => stdClass Object (
[Id] => 18895724
[AttributeId] => 147
[MetaType] => String
[Value] => stdClass Object (
[Data] => +46
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 2
[Language] => 0
)
[6] => stdClass Object (
[Id] => 18895725
[AttributeId] => 145
[MetaType] => String
[Value] => stdClass Object (
***[Data] => (
0410-1111111
)
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 2
[Language] => 0
)
[7] => stdClass Object (
[Id] => 18895728
[AttributeId] => 526
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 1
[Language] => 0
)
[8] => stdClass Object (
[Id] => 27970129
[AttributeId] => 115
[MetaType] => Media
[Value] => stdClass Object (
[MediaList] => stdClass Object (
[MediaObject] => stdClass Object (
[MediaId] => 41315620
[MediaType] => Image
[Url] => http://images.pix.se
[Width] => 214
[Height] => 317
[ProducedBy] =>
[CopyrightBy] =>
[ImageType] => Image
)
)
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 10
[Language] => 0
)
[9] => stdClass Object (
[Id] => 27970130
[AttributeId] => 418
[MetaType] => MultiAttribute
[Value] => stdClass Object (
[Values] => stdClass Object (
[MultiAttributeObject] => Array (
[0] => stdClass Object (
[Id] => 0
[AttributeId] => 0
[ProductId] => 0
[OrganizationId] => 0
[MultiAttributeId] => 631
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
[1] => stdClass Object (
[Id] => 0
[AttributeId] => 0
[ProductId] => 0
[OrganizationId] => 0
[MultiAttributeId] => 36
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
[2] => stdClass Object (
[Id] => 0
[AttributeId] => 0
[ProductId] => 0
[OrganizationId] => 0
[MultiAttributeId] => 2030
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
)
)
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 4
[Language] => 0
)
[10] => stdClass Object (
[Id] => 27970131
[AttributeId] => 668
[MetaType] => MultiAttribute
[Value] => stdClass Object (
[Values] => stdClass Object (
[MultiAttributeObject] => Array (
[0] => stdClass Object (
[Id] => 0
[AttributeId] => 668
[ProductId] => 445946
[OrganizationId] => 1045
[MultiAttributeId] => 2022
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
[1] => stdClass Object (
[Id] => 0
[AttributeId] => 668
[ProductId] => 445946
[OrganizationId] => 1045
[MultiAttributeId] => 2023
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
[2] => stdClass Object (
[Id] => 0
[AttributeId] => 668
[ProductId] => 445946
[OrganizationId] => 1045
[MultiAttributeId] => 2174
[MetaType] => Boolean
[Value] => stdClass Object (
[Data] =>
)
[Language] => 0
)
)
)
)
[ProductId] => 445946
[OrganizationId] => 1045
[Version] => 5
[Language] => 0
)
)
)
[Categories] => stdClass Object (
[Category] => stdClass Object (
[Id] => 16015
[OrganizationId] => 1045
[Name] => Film
[DefaultTemplateId] => 81
[Status] => Active
[ParentCategoryId] => 3568
[DisplayOrder] => 99
[Icon] =>
)
)
[GeoNode] => stdClass Object (
[Id] => 3558
[Name] => Staden med omnejd
[ParentId] => 66876
[OrgId] => 1045
[Type] => Undefined
[LeftValue] => 11
[RightValue] => 12
[CityCenterLatitude] =>
[CityCenterLongitude] =>
[CityCenterRadius] =>
)
[Occasions] => stdClass Object (
[OccasionObject] => stdClass Object (
[Id] => 19836976
[AttributeId] => 111
[ProductId] => 445946
[OrganizationId] => 1045
***[StartDate] => 2014-11-12T00:00:00
***[EndDate] => 2014-11-13T00:00:00
***[StartTime] => 0001-01-01T19:00:00
[EndTime] => 0001-01-01T19:00:00
***[ValidDays] => Wednesday Thursday
***[ArenaId] => 75316
***[ArenaName] => Moviegrafteatern Movie, Staden
[EntryTime] => 0001-01-01T00:00:00
)
)
[SupplierId] =>
[Duration] =>
[ForeignProductIds] => stdClass Object (
)
[LanguageId] => 1
)
)
我希望最终裁剪的json看起来像这样:
{
"Id":445946,
"Name": "Movie: Film",
"Image":{
"Url":"http://images.pix.se",
"Width":595,
"Height":709
},
"MovieUrl": "http://www.Movie.se",
"LongText": "Text text info tinfo text Long text",
"Price": "70 SEK",
"ShortText": "Short info text text text",
"Phone": "0410-1111111",
"StartDate": 2014-11-12T00:00:00,
"EndDate": 2014-11-13T00:00:00,
"StartTime": 0001-01-01T19:00:00,
"ValidDays": "Wednesday Thursday",
"ArenaId": 75316,
"ArenaName": "Moviegrafteatern Movie, Staden"
}
如何将此数组转换为更小的JSON?
答案 0 :(得分:1)
我认为解决问题的唯一方法是在函数中对所需项目进行exakt映射,然后循环遍历不同的对象,因为你使用这些不同的对象得到的结构非常糟糕。
function cleanUpResult(array)
{
$r = array();
foreach($item in $array){
$result = array();
$result["Id"] = $item["Id"];
$result["Name"] = $item["Name"];
$result["Image"] = array();
$result["Image"]["Url"] =$item["Image"]->Url;
$result["Image"]["Width"] =$item["Id"]->Width;
$result["Image"]["Height"] =$item["Id"]->Height;
$result["MovieUrl"] = $item["Attributes"]->AttributeData[0]->Value->Data;
$result["LongText"] = $item["Attributes"]->AttributeData[1]->Value->Data;
$result["Price"] = $item["Attributes"]->AttributeData[2]->Value->Data;;
$result["ShortText"] = $item["Attributes"]->AttributeData[4]->Value->Data;
$result["Phone"] = $item["Attributes"]->AttributeData[6]->Value->Data;
$result["StartDate"] = $item["StartDate"];
$result["EndDate"] = $item["EndDate"];
$result["StartTime"] = $item["StartTime"];
$result["ValidDays"] = $item["ValidDays"];
$result["ArenaId"] = $item["ArenaId"];
$result["ArenaName"] = $item["ArenaName"];
$r[] = $result;
}
return json_encode($r);
}
答案 1 :(得分:0)
只需创建要列入白名单的数组键列表,然后删除其余列表。
假设您的对象是$ object
//this line must come before any output is made. (echo, print, etc.)
header('Content-Type: application/json');
//whitelist. the items you want to keep
$keep = array("id", "name", "image" ...);
foreach($object as $object) {
foreach ($object as $key => $product) {
if( !in_array($key, $keep) ) { //check if the current loop key is not in the whitelist
unset($object[$key]);
}
}
}
//Then convert the array to JSon.
echo json_encode($object)
然后$ object将只保留白名单中的内容
答案 2 :(得分:0)
这并不复杂,但由于您需要嵌套数组中的各种字段,因此最简单的方法是迭代数组并获取要编码的各个字段。我不确定构建一个聪明的过滤器是否值得花时间。
例如,假设$soapArray
包含您在上面提供的信息:
$data = array();
// iterate over the full field list
foreach ($soapArray as $soapElement) {
// construct a partial array containing the fields you want
$data[] = array(
'Id' => $soapElement['Id'],
'Name' => $soapElement['Name'],
'Image' => array(
'Url' => $soapElement['Image']->Url,
'Width' => $soapElement['Image']->Width,
'Height' => $soapElement['Image']->Height,
),
'MovieUrl' => $soapElement['Attributes']->AttributeData[0]->Value->Data,
'LongText' => $soapElement['Attributes']->AttributeData[1]->Value->Data,
);
}
// display the encoded array
echo json_encode($data);