У меня есть файл 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
      }
   ]
}

После декодирования в Array с использованием цикла foreach это выглядит так:

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
    )

)

Теперь, как я могу индивидуально декодировать и отображать nodeName для CategoryName.

0
Rashmi Sonke 5 Янв 2016 в 19:21

2 ответа

Лучший ответ

С помощью некоторого 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 ) . ']' );
0
ElChiniNet 5 Янв 2016 в 19:57

Содержимое CategoryName не является стандартным форматом, как сказал Niet the Dark Absol.

Вот способ получить идентификатор узла и название категории. Я боюсь, что это не лучший способ сделать это, но хочу сказать, что это просто помочь вам.

Ideone

$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/>';
}
0
Uttam Kumar Roy 6 Янв 2016 в 07:32