1

I've a got this string after parsing webpage. It looks something similar to this, which is not a valid format of JSON. I was wondering if I can parse this string to get desired values.

string(4747) " _result_ ={status:"OK",loc:"India",
country:{regions:[{ap:"755",mp:"768"},{up:"125",mh:"188"}]},src:"gov"}"

Here you can observe that, the fieldNames doesn't have quotes at all. I've tried to json_decode() which returned NULL. So, Please help me in parsing this string. Thank you.

5
  • Where are you getting this string? Do you have control of the source (i.e. the thing that parses your webpage?). If so, change that to return a decent format, so that future you doesn't have to struggle with it. Commented Nov 22, 2013 at 19:57
  • Try this: stackoverflow.com/questions/1575198/… Commented Nov 22, 2013 at 19:57
  • json_decode(substr($x,11),true) Commented Nov 22, 2013 at 20:00
  • @MadaraUchiha No, I don't have access to the source. It was from an external website. Commented Nov 22, 2013 at 20:25
  • @vinnylinux Unfortunately,That didn't work for me. Commented Nov 22, 2013 at 20:26

3 Answers 3

0

The field names don't need to have quotes if they are just single-word strings. The problem is likely right at the beginning:

" _result_ ="

That's the problem. Try str_replace(' _result_ =', '', $string); and see if it will decode then.

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

Comments

0

It looks like you can simply remove any portion of the string up to the first {

$raw_string = '...'; // your string to cleanse
$json = substr($raw_string, strpos($raw_string, '{'));
$object = json_decode($json);

4 Comments

I think, this is almost the same idea as @Travesty3. I got the same NULL.
If you manuually remove that part from the string and run it though JSONLint or similar, to you get valid JSON?
Yes, I tries that. It said, there should be quotes over fieldNames. Thats what I mentioned in the question earlier.
@VishwaDatta Sorry I missed that part. You either need to change the JSON that is created (if you have control over this) or you might need to look into another library to use for JSON decoding that is more leninent.
0

What you got looks a bit like JSONP (but isn't). Mostly a webservice offers parameters to either set the prefix (padding) or even omit it. If not try to replace it before paring as json. Also you need to replace the trailing ;.. (not shown in question but should be present):

// replace the padding
$input = str_replace(' _result_ =', '', $input);

// replace the trailing ';'
$input = rtrim($input, ";")

// now parse as json
var_dump(json_decode($input));

5 Comments

The rtrim() works gracefully even if there is no trailing ;. Does the solution work for you?
No, Its not working. I think, rtrim() has nothing to do here. Therefore,it is same as the one I tried already.
Are you sure you know what you are doing?.. Firstly, it seems you have no plan about the data you received from the web site. Have you read their documentation? Secondly rtrim() just replaces the ; if it is present. So why do you complain? Thirdly: how should I solve this without more information?...
1) They don't have a documentation. I just parsed a page on their which is written this way. 2) Yes, I know rtrim() replaces only if it exists. But as there is know ; in the code. It does no work. Isn't it? 3) I just want to parse that string. For eg: If I ask for status, it should return OK.
Then I assume they don't want you leaking their data + you have no understanding about how to do it anyway... Good night

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.