简体   繁体   中英

Validate JSON with json schema is failing

Hi I need to do a school assigement with a API thats generating json.

I get with $API my json. With some testing by myself I can say that the json is correct. But in the assigement it says I need to validate it with a json schema. I have the schema but I cant get it to work so it will check and validate the incomming json.

If someone sees the problem pls tell me because I cant find it.

                <?php
                    //Json gets validated
                    if(isset($API))
                    {                   
                        ?>
                            <script src="//cdnjs.cloudflare.com/ajax/libs/validate.js/0.13.1/validate.min.js">
                            var validator = require('validator');
                            var jsv = require('json-validator');
                            jsv.validate("<?php echo $API; ?>", "json_schema.json", function(err, messages)) {
                                    if(err) 
                                    {
                                        throw err;
                                    }
                                    else
                                    {
                                        $.getJSON("<?php echo $API; ?>", function(data) 
                                        {
                                            var items = [];
                                            $.each(data, function(key, val, val2) {
                                                items.push("<li id='" + key + "'>" + val["COL 3"] + "</li>");
                                                items.push("<br>");
                                            });

                                            $("<ul/>", {
                                                "class": "my-new-list",
                                                html: items.join("")
                                            }).appendTo(".datapanel");
                                        });
                                    }
                                }
                            </script>
                        <?php
                    }
                ?>

Replace both <?php echo $API; ?> <?php echo $API; ?> by <?php echo str_replace('"', '\\"', $API); ?> <?php echo str_replace('"', '\\"', $API); ?> .

Even better, you could have this process once and then echo the escaped string:

<?php
    // Json gets validated
    if (isset($API))
    {
        // escape your JSON string
        $escapedAPI = str_replace('"', '\\"', $API);
        ?>

        ...

        <!-- echo the escaped string -->
        <?php echo $escapedAPI; ?>

        ...

        <?php
    }
?>

The issue you're facing is that currenty, when PHP echo the JSON in your Javascript, it produces something like this:

jsv.validate("[{"COL 3":"(APPLAUSE)"}, ... ]", "json_schema.json", function() { ... })

As you can see, the " from the Javascript are mixed with the one "echoed" which produce invalid Javascript. This is why you need to escape them before "echoing" your JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM