-1

I have a problem that I can not solve. JQuery works with getJSON, php does not work with foreach. The error I get in PHP:

Notice: Trying to get property of non-object in C:\xampp\htdocs\plreserv\index.php on line 14 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\plreserv\index.php on line 14

JSON output from the service:

{
    dataList: [
        {
            Date:'11.08.2017',
            Com:'Spr',
            BlsId:'5',
            Type:'break',
            TypeCheck:'0,5',
            perma:'Normal',
            CommaDetail:'0,,,particle,0,,,7',
            Pom:[
                {
                    Num:'1',
                    status:'1',
                    val:'37349',
                    lang:'tr'
                },
                {
                    Num:'2',
                    status:'1',
                    val:'37350',
                    lang:'ru'
                }
            ]
        }
    ]
}

My php code:

$file = file_get_contents(url);
$json = json_decode($file);
foreach($json as $data):
// code
endforeach;

There are 18 pages like this and none of them work in PHP. This is the error I get from the JSON viewer:

Image attachment

I'm sorry for my bad english. Thanks.

Valkyrie
  • 1
  • 1
  • 1
    What is shown is not valid JSON. Validate it on jsonlint.com. Beyond that you need to show the code that is not working also – charlietfl Aug 12 '17 at 11:03
  • And the PHP you use is? – SuperDJ Aug 12 '17 at 11:03
  • ^ this, and can you show us your php code with the foreach in it? – Jaeger Aug 12 '17 at 11:04
  • 1
    Welcome to Stack Overflow, please take a time to go through [the welcome tour](https://stackoverflow.com/tour) to know your way around here (and also to earn your first badge), read how to [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and also check [How to Ask Good Questions](https://stackoverflow.com/help/how-to-ask) so you increase your chances to get feedback and useful answers. – Josef Adamcik Aug 12 '17 at 11:04
  • $file = file_get_json("http://localhost:4000/otelservice/"); $json = json_decode($file); foreach($json as $data): endforeach; my php code. – Valkyrie Aug 12 '17 at 11:09
  • 1
    Don't add the code as a comment, update your question instead. – localheinz Aug 12 '17 at 11:11
  • Possible duplicate of [Returning JSON from a PHP Script](https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script) – Daniel Beck Aug 12 '17 at 18:17

1 Answers1

0

This is the right format of your json:

[
{
    "Date":"11.08.2017",
    "Com":"Spr",
    "BlsId":"5",
    "Type":"break",
    "TypeCheck":"0,5",
    "perma":"Normal",
    "CommaDetail":"0,,,particle,0,,,7",
    "Pom":[
        {
            "Num":"1",
            "status":"1",
            "val":"37349",
            "lang":"tr"
        },
        {
            "Num":"2",
            "status":"1",
            "val":"37350",
            "lang":"ru"
        }
    ]
}]

all inside json must be wrapped by double quote (except integer, you haven't)

Luki Centuri
  • 185
  • 1
  • 6