#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2017
    Posts
    6
    Rep Power
    0

    Issue converting JSON to PHP


    Im trying to convert a JSON feed into PHP but coming up against an issue:

    This is the feed:

    Code:
    {"consumer_reviews_api": {"reviews": [{"value_rating": "5.0", "found_trader_on_trustatrader": false, "punctuality_rating": "5.0", "trader_reply": "", "would_recommend": true, "date_of_work": "29th March 2018", "comments": "Utility pros have worked with us on various projects over the last 4 months  whilst we are undertaking renovations and have been nothing short of EXCELLENT. Despite having new 'items' added almost on a weekly basis they have undertaken every task with professionalism and a willingness to get the job done. I would strongly recommend this company for efficient, friendly and professional service", "quality_rating": "5.0", "average_rating": "5.00", "reviewer_name": "Martin Chick", "initial_impression_rating": "5.0", "cleanliness_rating": "5.0", "id": 279103, "overall_rating": "5.0"}, {"value_rating": "5.0", "found_trader_on_trustatrader": false, "punctuality_rating": "5.0", "trader_reply": "", "would_recommend": true, "date_of_work": "26th March 2018", "comments": "Haven\u2019t had the bill yet but previous work fairly priced. Feel very confident in his work", "quality_rating": "5.0", "average_rating": "5.00", "reviewer_name": "Eurem Matthews", "initial_impression_rating": "5.0", "cleanliness_rating": "5.0", "id": 278800, "overall_rating": "5.0"}, {"value_rating": "5.0", "found_trader_on_trustatrader": true, "punctuality_rating": "5.0", "trader_reply": "", "would_recommend": true, "date_of_work": "14th March 2018", "comments": "Ian was great, arrived on time, carried out work neatly, swiftly and cleanly and even kindly did a little extra that I only briefed in when he arrived.  Ian fitted two new sink wastes, removed and refitted a radiator and resealed a wet area for me. Good value, I'll use him again and would definitely recommend.", "quality_rating": "5.0", "average_rating": "5.00", "reviewer_name": "E Lowndes", "initial_impression_rating": "5.0", "cleanliness_rating": "5.0", "id": 279725, "overall_rating": "5.0"}, {"value_rating": "5.0", "found_trader_on_trustatrader": false, "punctuality_rating": "5.0", "trader_reply": "", "would_recommend": true, "date_of_work": "8th March 2018", "comments": "This is the second time we have used Utility Pros and would highly recommend Ian. He carried out a routine service of our gas boiler and was very thorough and helpful; he even fixed a problem with one of the radiators for us while he was here. Everywhere was left really tidy afterwards and the boiler was noticeably quieter after the service.", "quality_rating": "5.0", "average_rating": "5.00", "reviewer_name": "Charlie  Hamilton ", "initial_impression_rating": "5.0", "cleanliness_rating": "5.0", "id": 278705, "overall_rating": "5.0"}, {"value_rating": "4.0", "found_trader_on_trustatrader": true, "punctuality_rating": "5.0", "trader_reply": "", "would_recommend": true, "date_of_work": "5th March 2018", "comments": "Utility Pros completed the installation of new Grant Oil Boiler, replacement of two radiators and fine tuning the radiators balancing. In summary Ian always arrived at the agreed time and worked long hours to ensure the job was completed to schedule. He is polite, professional and very tidy in his work. Of particular note Ian is responsive to any follow-up work required after the initial installation to ensure the system is working as it should be. Client satisfaction is at the core of his business and I have no hesitation in recommending Ian (Utility Pro\u2019s) for all heating related work.", "quality_rating": "5.0", "average_rating": "4.83", "reviewer_name": "Vaughn Sadler", "initial_impression_rating": "5.0", "cleanliness_rating": "5.0", "id": 279727, "overall_rating": "5.0"}], "trader": {"total_page_count": 7, "average_rating": "4.93", "company_name": "Utility Pros UK Limited", "total_review_count": 35}, "params": {"username": "UTILITYPROS01", "page": 1, "reviews_per_page": 5}}}
    This is the PHP code:

    PHP Code:
                                                 foreach ($characters as $character) {
                                                          
    $rating $character->overall_rating;
                                                          
    $name $character->reviewer_name;
                                                          
    $comments $character->comments;
                                                          
    $date $character->date_of_work;
                                                          

                                                  
    $reviewNum++;
                                            } 

    The problem im having is with the first part of the JSON feed:

    {"consumer_reviews_api": {"reviews":

    When this part isnt included in the feed, all the above works fine. But this is part of the feed (unfortunatly) and I dont understand how to modify the PHP code to see past this.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,472
    Rep Power
    653
    You need to show all your code. Show how you are loading the JSON string. Specifically how you are using json_decode to create the PHP associative array.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,961
    Rep Power
    4575
    foreach should be this:

    PHP Code:
    foreach ($characters->consumer_reviews_api->reviews as $character) { 
    -- Cigars, whiskey and wild, wild women. --
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Location
    Pakistan
    Posts
    38
    Rep Power
    1
    $url = "http://localhost/rest/users"; //The url from where you are getting the contents
    $response = (file_get_contents($url)); //Converting in json string
    $n = strpos($response, "[");
    $response = substr_replace($response,"",0,$n+1);
    $response = substr_replace($response, "" , -1,1);
    print_r(json_decode($response,true));
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Location
    Pakistan
    Posts
    38
    Rep Power
    1
    <?php
    $contents = file_get_contents($url);
    $contents = utf8_encode($contents);
    $results = json_decode($contents);
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo