0

I am trying to parse this json '{"options":"[{\"order\":\"1\"},{\"skill\":\"\\n wajghjk \"},{\"order\":\"2\"},{\"skill\":\"\\n aswedrty \"},{\"order\":\"3\"},{\"skill\":\"\\n skill1 \"},{\"order\":\"4\"},{\"skill\":\"\\n wasedjk \"},{\"order\":\"5\"},{\"skill\":\"\\n Feldsher \"},{\"order\":\"6\"},{\"skill\":\"\\n wasedjk \"},{\"order\":\"7\"},{\"skill\":\"\\n Autotransfusionist \"},{\"order\":\"8\"},{\"skill\":\"\\n Feldsher \"},{\"order\":\"9\"},{\"skill\":\"\\n Autotransfusionist \"}]"}' . using the code

<?php
$json = '{"options":"[{\"order\":\"1\"},{\"skill\":\"\\n                        wajghjk                    \"},{\"order\":\"2\"},{\"skill\":\"\\n                        aswedrty                    \"},{\"order\":\"3\"},{\"skill\":\"\\n                        skill1                    \"},{\"order\":\"4\"},{\"skill\":\"\\n                        wasedjk                    \"},{\"order\":\"5\"},{\"skill\":\"\\n                        Feldsher                    \"},{\"order\":\"6\"},{\"skill\":\"\\n                        wasedjk                    \"},{\"order\":\"7\"},{\"skill\":\"\\n                        Autotransfusionist                    \"},{\"order\":\"8\"},{\"skill\":\"\\n                        Feldsher                    \"},{\"order\":\"9\"},{\"skill\":\"\\n                        Autotransfusionist                    \"}]"}';

$decode = json_decode($json, true);
$a=array();
echo '<br/>';
foreach($decode['options'] as $a)
{
    echo $a['order'];echo '<br/>';
     echo $a['skill'];echo '<br/>';
}
?>

But it is showing error and not able to parse . Is there any better way to get the values of order and skill from this json ?

6
  • 1
    What error do you get? Commented Jul 23, 2013 at 12:18
  • Could you link the error? Commented Jul 23, 2013 at 12:18
  • 1
    You should remove the backslashes Commented Jul 23, 2013 at 12:18
  • if you paste it in jsonviewer.stack.hu you'll see what you have isn't well-formed JSON. Commented Jul 23, 2013 at 12:20
  • Iam getting the error as Warning: Invalid argument supplied for foreach() on line 12 Commented Jul 23, 2013 at 12:24

4 Answers 4

3

That's not JSON. You are escaping too many double quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

Check this out to see whether you have valid JSON or not: json.parser.online.fr
1

You're escaping your double quotes, which will come out as literal \ characters when the string is delimited by single quotes.

Either run stripslashes on the json string, or avoid the escaping in the first place.

Comments

1

You have two problems with the json string.

  1. You escape double quotes inside single-quoted string. But single-quoted string has only one escape sequence and that is \'. Check this article.
  2. You have a problem here: '{"options":"[{...}]"}'. You enclosed javascript [array] with quotes, which turned it into a string.

The correct json should look like this:

{"options":[{"order":"1"},{"skill":"\\n wajghjk "},{"order":"2"},{"skill":"\\n aswedrty "},{"order":"3"},{"skill":"\\n skill1 "},{"order":"4"},{"skill":"\\n wasedjk "},{"order":"5"},{"skill":"\\n Feldsher "},{"order":"6"},{"skill":"\\n wasedjk "},{"order":"7"},{"skill":"\\n Autotransfusionist "},{"order":"8"},{"skill":"\\n Feldsher "},{"order":"9"},{"skill":"\\n Autotransfusionist "}]}

Eval.in demo

Comments

1

it is showing error and not able to parse .

PHP is quite weak in debugging json. All you can do is

$json_errors = array(
    "",
    "The maximum stack depth has been exceeded",
    "Invalid or malformed JSON",
    "Control character error, possibly incorrectly encoded",
    "Syntax error",
    "Malformed UTF-8 characters, possibly incorrectly encoded",
);

$decode = json_decode($json, true);
if ($error = json_last_error())
{
    trigger_error($json_errors[$error]);
}

Though, in order to get values you have to have a valid json string, not just arbitrary string consists of some quotes and slashes.

And of course you should never ever create a json string manually.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.