我有一个json文件。
{
"GetProductsByCategoryResult":[
{
"Brand":"Bohra",
"CategoryName":"[[{nodeId=20001, nodeName=FLIPKART_TREE}, {nodeId=20007, nodeName=Electronics}, {nodeId=20268, nodeName=Appliances}, {nodeId=20098, nodeName=Kitchen Appliances}, {nodeId=20110, nodeName=Electric Jug(heater)\/Travel Kettles}]]",
"CategoryPathAsString":"Root|[[{nodeId=20001, nodeName=FLIPKART_TREE}, {nodeId=20007, nodeName=Electronics}, {nodeId=20268, nodeName=Appliances}, {nodeId=20098, nodeName=Kitchen Appliances}, {nodeId=20110, nodeName=Electric Jug(heater)\/Travel Kettles}]]|",
"Colour":null,
"Custom1":"40",
"Custom2":"0",
"Custom3":null,
"Custom4":null,
"Custom5":null,
"DiscountedPrice":0.00,
"Location":null,
"MID":678106,
"MPN":null,
"MerchantName":"Flipkart",
"PID":13653,
"ProductDescription":"Ideal for boiling water, maggie this electric kettle from Bohra Mrketing is a necessity for every household.",
"ProductID":123802338,
"ProductImageLargeURL":"http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-400x400-imae4dcyxpvrbqbu.jpeg;http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-75x75-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-275x275-imae4dcyxpvrbqbu.jpeg;http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-125x125-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-40x40-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-original-imae4dcyxpvrbqbu.jpeg;http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-1100x1100-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-100x100-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-200x200-imae4dcyxpvrbqbu.jpeg;",
"ProductImageMediumURL":null,
"ProductImageSmallURL":null,
"ProductName":"Bohra Bht013 Rose 1 L Electric Kettle",
"ProductPrice":1599.00,
"ProductPriceCurrency":"INR",
"ProductSKU":"EKTE73GBRPMVYWNH",
"ProductURL":"http:\/\/clk.omgt5.com\/?AID=861823&PID=13653&Type=12&r=http:\/\/dl.flipkart.com\/dl\/bohra-bht013-rose-1-l-electric-kettle\/p\/itme73gbhjxf5hty%3Fpid%3DEKTE73GBRPMVYWNH",
"StockAvailability":"In stock",
"WasPrice":2700.00
}
]
}
使用foreach循环在Array
中解码之后,它看起来像这样:
Array(
[0] => Array
(
[Brand] => Westinghouse
[CategoryName] => [[{nodeId=20001, nodeName=FLIPKART_TREE}, {nodeId=20007, nodeName=Electronics}, {nodeId=20268, nodeName=Appliances}, {nodeId=20098, nodeName=Kitchen Appliances}, {nodeId=20110, nodeName=Electric Jug(heater)/Travel Kettles}]]
[CategoryPathAsString] => Root|[[{nodeId=20001, nodeName=FLIPKART_TREE}, {nodeId=20007, nodeName=Electronics}, {nodeId=20268, nodeName=Appliances}, {nodeId=20098, nodeName=Kitchen Appliances}, {nodeId=20110, nodeName=Electric Jug(heater)/Travel Kettles}]]|
[Colour] =>
[Custom1] => 15
[Custom2] => 0
[Custom3] =>
[Custom4] =>
[Custom5] =>
[DiscountedPrice] => 0
[Location] =>
[MID] => 678106
[MPN] =>
[MerchantName] => Flipkart
[PID] => 13653
[ProductDescription] =>
[ProductID] => 123803505
[ProductImageLargeURL] => http://img5a.flixcart.com/image/electric-kettle/e/c/4/westinghouse-k708-k708-original-imad7ywryyk4rjjk.jpeg;http://img5a.flixcart.com/image/electric-kettle/e/c/4/westinghouse-k708-k708-400x400-imad7ywryyk4rjjk.jpeg;http://img5a.flixcart.com/image/electric-kettle/e/c/4/westinghouse-k708-k708-75x75-imad7ywryyk4rjjk.jpeg;http://img6a.flixcart.com/image/electric-kettle/e/c/4/westinghouse-k708-k708-275x275-imad7ywryyk4rjjk.jpeg;http://img5a.flixcart.com/image/electric-kettle/e/c/4/westinghouse-k708-k708-125x125-imad7ywryyk4rjjk.jpeg;http://img5a.flixcart.com/image/electric-kettle/e/c/4/westinghouse-k708-k708-40x40-imad7ywryyk4rjjk.jpeg;http://img5a.flixcart.com/image/electric-kettle/e/c/4/westinghouse-k708-k708-100x100-imad7ywryyk4rjjk.jpeg;http://img6a.flixcart.com/image/electric-kettle/e/c/4/westinghouse-k708-k708-200x200-imad7ywryyk4rjjk.jpeg;
[ProductImageMediumURL] =>
[ProductImageSmallURL] =>
[ProductName] => Westinghouse K708 2 Electric Kettle
[ProductPrice] => 2278
[ProductPriceCurrency] => INR
[ProductSKU] => EKTD7YPQ9NHFTEC4
[ProductURL] => http://clk.omgt5.com/?AID=861823&PID=13653&Type=12&r=http://dl.flipkart.com/dl/westinghouse-k708-2-electric-kettle/p/itmd7yprd38phkch%3Fpid%3DEKTD7YPQ9NHFTEC4
[StockAvailability] => Out of stock
[WasPrice] => 2690
)
)
现在我如何解析和显示CategoryName的nodeName。
答案 0 :(得分:0)
CategoryName内容不是任何标准格式,如Dark Absol说的Niet。
这是一种获取Node ID& amp;分类名称。我担心这不是最好的方法,但我想说它只是为了帮助你。
$json_data='{"GetProductsByCategoryResult":[{"Brand":"Bohra","CategoryName":"[[{nodeId=20001, nodeName=FLIPKART_TREE}, {nodeId=20007, nodeName=Electronics}, {nodeId=20268, nodeName=Appliances}, {nodeId=20098, nodeName=Kitchen Appliances}, {nodeId=20110, nodeName=Electric Jug(heater)\/Travel Kettles}]]","CategoryPathAsString":"Root|[[{nodeId=20001, nodeName=FLIPKART_TREE}, {nodeId=20007, nodeName=Electronics}, {nodeId=20268, nodeName=Appliances}, {nodeId=20098, nodeName=Kitchen Appliances}, {nodeId=20110, nodeName=Electric Jug(heater)\/Travel Kettles}]]|","Colour":null,"Custom1":"40","Custom2":"0","Custom3":null,"Custom4":null,"Custom5":null,"DiscountedPrice":0.00,"Location":null,"MID":678106,"MPN":null,"MerchantName":"Flipkart","PID":13653,"ProductDescription":"Ideal for boiling water, maggie this electric kettle from Bohra Mrketing is a necessity for every household.","ProductID":123802338,"ProductImageLargeURL":"http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-400x400-imae4dcyxpvrbqbu.jpeg;http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-75x75-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-275x275-imae4dcyxpvrbqbu.jpeg;http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-125x125-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-40x40-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-original-imae4dcyxpvrbqbu.jpeg;http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-1100x1100-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-100x100-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-200x200-imae4dcyxpvrbqbu.jpeg;","ProductImageMediumURL":null,"ProductImageSmallURL":null,"ProductName":"Bohra Bht013 Rose 1 L Electric Kettle","ProductPrice":1599.00,"ProductPriceCurrency":"INR","ProductSKU":"EKTE73GBRPMVYWNH","ProductURL":"http:\/\/clk.omgt5.com\/?AID=861823&PID=13653&Type=12&r=http:\/\/dl.flipkart.com\/dl\/bohra-bht013-rose-1-l-electric-kettle\/p\/itme73gbhjxf5hty%3Fpid%3DEKTE73GBRPMVYWNH","StockAvailability":"In stock","WasPrice":2700.00}]}';
$array_data = json_decode( $json_data, true );
// Get Category Name Data in a variable
$category_data = $array_data['GetProductsByCategoryResult'][0]['CategoryName'];
// Remove '[[' && ']]' string from start and end of the string respectively
$category_data =str_replace('[[', '', $category_data);
$category_data =str_replace(']]', '', $category_data);
// Extract ( Explode ) all rows by '}'
$cat_array = explode("},", $category_data);
// Get all rows by for loop
foreach ( $cat_array as $key => $cRow ) {
// Extract ( Explode ) category name rows by 'nodeName='
$node_array = explode("nodeName=", $cRow);
// Get nodeId from extracted array
$nodeId = $node_array[0];
// Get category name from extracted array
$cat_name = $node_array[1];
// Remove 'nodeId', ',' , '{' & '}' char from string
$nodeId =str_replace('nodeId=', '', $nodeId);
$nodeId =str_replace('{', '', $nodeId);
$nodeId =str_replace('{', '', $nodeId);
$nodeId =str_replace(',', '', $nodeId);
// Remove {' & '}' from string
$cat_name =str_replace('{', '', $cat_name);
$cat_name =str_replace('}', '', $cat_name);
// Show Node Id & Category Name
echo 'Node:<b>';
echo $nodeId;
echo '</b> <--->';
echo 'Category Name:<b>';
echo $cat_name;
echo '</b><br/>';
}
答案 1 :(得分:0)
对于某些RegExps
,您可以将String
转换为正确的json
格式,然后对其进行解码:
PHP代码
$json = '{"GetProductsByCategoryResult":[{"Brand":"Bohra","CategoryName":"[[{nodeId=20001, nodeName=FLIPKART_TREE}, {nodeId=20007, nodeName=Electronics}, {nodeId=20268, nodeName=Appliances}, {nodeId=20098, nodeName=Kitchen Appliances}, {nodeId=20110, nodeName=Electric Jug(heater)\/Travel Kettles}]]","CategoryPathAsString":"Root|[[{nodeId=20001, nodeName=FLIPKART_TREE}, {nodeId=20007, nodeName=Electronics}, {nodeId=20268, nodeName=Appliances}, {nodeId=20098, nodeName=Kitchen Appliances}, {nodeId=20110, nodeName=Electric Jug(heater)\/Travel Kettles}]]|","Colour":null,"Custom1":"40","Custom2":"0","Custom3":null,"Custom4":null,"Custom5":null,"DiscountedPrice":0.00,"Location":null,"MID":678106,"MPN":null,"MerchantName":"Flipkart","PID":13653,"ProductDescription":"Ideal for boiling water, maggie this electric kettle from Bohra Mrketing is a necessity for every household.","ProductID":123802338,"ProductImageLargeURL":"http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-400x400-imae4dcyxpvrbqbu.jpeg;http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-75x75-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-275x275-imae4dcyxpvrbqbu.jpeg;http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-125x125-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-40x40-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-original-imae4dcyxpvrbqbu.jpeg;http:\/\/img5a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-1100x1100-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-100x100-imae4dcyxpvrbqbu.jpeg;http:\/\/img6a.flixcart.com\/image\/electric-kettle\/w\/n\/h\/bohra-bht013-rose-200x200-imae4dcyxpvrbqbu.jpeg;","ProductImageMediumURL":null,"ProductImageSmallURL":null,"ProductName":"Bohra Bht013 Rose 1 L Electric Kettle","ProductPrice":1599.00,"ProductPriceCurrency":"INR","ProductSKU":"EKTE73GBRPMVYWNH","ProductURL":"http:\/\/clk.omgt5.com\/?AID=861823&PID=13653&Type=12&r=http:\/\/dl.flipkart.com\/dl\/bohra-bht013-rose-1-l-electric-kettle\/p\/itme73gbhjxf5hty%3Fpid%3DEKTE73GBRPMVYWNH","StockAvailability":"In stock","WasPrice":2700.00}]}';
$obj = json_decode($json);
$str = $obj->{'GetProductsByCategoryResult'}[0]->{'CategoryName'};
$obj->{'GetProductsByCategoryResult'}[0]->{'CategoryName'} = json_decode( '[' . preg_replace( ['/([^=\[\],\s\{\}]+)=([^=\[\],\{\}]+)/', '/(Root|\||\[|\])/'], ['"$1":"$2"'], $str ) . ']' );
CategoryName
属性将具有此值:
CategoryName的值:
Array
(
[0] => stdClass Object
(
[nodeId] => 20001
[nodeName] => FLIPKART_TREE
)
[1] => stdClass Object
(
[nodeId] => 20007
[nodeName] => Electronics
)
[2] => stdClass Object
(
[nodeId] => 20268
[nodeName] => Appliances
)
[3] => stdClass Object
(
[nodeId] => 20098
[nodeName] => Kitchen Appliances
)
[4] => stdClass Object
(
[nodeId] => 20110
[nodeName] => Electric Jug(heater)/Travel Kettles
)
)
使用相同的RegExp
,您还可以解码CategoryPathAsString
属性:
可选的PHP代码
$str = $obj->{'GetProductsByCategoryResult'}[0]->{'CategoryPathAsString'};
$obj->{'GetProductsByCategoryResult'}[0]->{'CategoryPathAsString'} = json_decode( '[' . preg_replace( ['/([^=\[\],\s\{\}]+)=([^=\[\],\{\}]+)/', '/(Root|\||\[|\])/'], ['"$1":"$2"'], $str ) . ']' );