Forums

json_decode returns null

kwok 12 Jul, 2015
Hi Max/Bob,

1. I followed http://www.chronoengine.com/faqs/70-cfv5/5251-how-can-i-build-a-product-list.html to create a json coded string and saved it to text field (namely items) of a table:
[{"buyerItem":"9","categoryID":"6","menuItem":"533","itemName":"Audi A6"},{"buyerItem":"11","categoryID":"7","menuItem":"533","itemName":"Mercedes CLS"}]


It is a valid json validated through http://jsonlint.com/
[
    {
        "buyerItem": "9",
        "categoryID": "6",
        "menuItem": "533",
        "itemName": "Audi A6"
    },
    {
        "buyerItem": "11",
        "categoryID": "7",
        "menuItem": "533",
        "itemName": "Mercedes CLS"
    }
]


2. Then I followed http://www.chronoengine.com/faqs/72-ccv5/5207-view-and-edit-actions.html to create a display list of the item s through chronoconnectivity as follows:

Offer ID Buyer User ID Cash Supplement Buyer Remarks Date Submitted Offer Status
48 730 200.00 remarks 2015-06-16 23:49:12 Open

Offer id is the primary key and is set view linkable. Upon click, the content of the items field is to be shown on a new page.

Per the How-To guide, value of the database field is displayed by {items}.

However, I need to do json_decode so I can restore the item list.

On Front List - Actions, I did the following:
<?php
$json="{swapOffers.buyer_items}";
echo $json;
$restore = json_decode($json, false);
var_dump($restore);
?>
The result is:
[{"buyerItem":"9","categoryID":"6","menuItem":"533","itemName":"Audi A6"},{"buyerItem":"11","categoryID":"7","menuItem":"533","itemName":"Mercedes CLS"}]

NULL

3. I changed the parm to a "true" and followed some google search suggestion but still always get a null.

Would you please advise what I have missed at your earliest convenience?

Thanks,
kwok
kwok 20 Jul, 2015
Hi Max/Bob,

I have tried several other ways based on some google search result but still receiving null.

I hope you can find time to shed some light on this as soon as possible. Help!

Thanks,
kwok
GreyHead 21 Jul, 2015
Hi kwok,

I would suggest that you link to a separate form to display the items list. If you pass the ID to the form you can read the record back there and use a custom code action to decode the string.

Bob
kwok 19 Aug, 2015
Hi Bob/Max,

Just out of curiosity, how come CC executes json_decode on a json validated data like this one:

[{"buyerItem":"9","categoryID":"6","menuItem":"533","itemName":"Audi A6"},{"buyerItem":"11","categoryID":"7","menuItem":"533","itemName":"Mercedes CLS"}]

to give a NULL <json_last_error() = 4 means syntax error>?

Thanks,
kwok
kwok 23 Jan, 2016
Hi Bob/Max,

Based on your recommendation, I have the following:
1. On Front List - Actions: I set the view event to form event as BuyerItems:load.
2. Create a chronoform known as BuyerItems.
3. The CC displays the following:
Offer ID Buyer User ID Cash Supplement Buyer Remarks Date Submitted Offer Status

48 730 200.00 remarks 2015-06-16 23:49:12 Open

Offer id is the primary key and is set view linkable. Upon click, below data is passed to the CF:

Array
(
    [option] => com_chronoconnectivity5
    [cont] => lists
    [ccname] => itemDetailSwapView
    [act] => view
    [gcb] => 48
    [swapOffers] => Array
        (
            [id] => 48
            [buyer_user_id] => 730
            [buyer_user_name] => test2
            [buyer_items] => [{"buyerItem":"9","categoryID":"6","menuItem":"533","itemName":"Audi A6"},{"buyerItem":"11","categoryID":"7","menuItem":"533","itemName":"Mercedes CLS"}]
            [buyer_cash] => 200.00
            [buyer_remarks] => remarks
       ....
        )

)

What should the custom code be to convert [buyer_items] into arrays? I tried json_decode but the result is always null.

Thanks,
kwok
GreyHead 23 Jan, 2016
Hi kwok,

I checked and Json_decode works OK with that json string.

<?php
$json = '[{"buyerItem":"9","categoryID":"6","menuItem":"533","itemName":"Audi A6"},{"buyerItem":"11","categoryID":"7","menuItem":"533","itemName":"Mercedes CLS"}]';
$json = json_decode($json, true);
?>
gives this
$json: Array ( 
  [0] => Array (
    [buyerItem] => 9 
    [categoryID] => 6
    [menuItem] => 533
    [itemName] => Audi A6
  ) 
  [1] => Array (
    [buyerItem] => 11
    [categoryID] => 7
    [menuItem] => 533
    [itemName] => Mercedes CLS
  )
)

Bob
kwok 23 Jan, 2016
Hi Bob,

I used this: $json is empty and the $result is always NULL.

<?php
$json = $form->swapOffers['buyer_items'];
$result = json_decode($json, true);
echo $json
var_dump($result);
?>

Must be the way $json is populated not good. What should it be like?

Thanks,
Kwok
GreyHead 24 Jan, 2016
Hi kowok,

In your code it would be
$json = $form->data['swapOffers']['buyer_items'];

Bob
kwok 24 Jan, 2016
Hi Bob,

You're absolutely right. json_decode works when the variable is set following your rule!

Now, upon click on the gcb link on the CC form (http://www.xxx.com/index.php?option=com_chronoconnectivity5&cont=lists&ccname=itemDetailSwapView&act=view&gcb=48), list of items are displayed perfectly on the same browser tab.

Is there a way to make the list of items to be displayed on a new browser tab instead? What needs to be done on the CC form?

Thanks a lot.
kwok
GreyHead 24 Jan, 2016
Hi Kwok,

You probably don't want to open the form in a new tab. If you do then you can set a target attribute on the link either by creating it manually, or by adding it to the CC created link using JavaScript.

Bob
This topic is locked and no more replies can be posted.