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.

Vishwa
  • 1,525
  • 2
  • 13
  • 25

3 Answers3

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.

Travesty3
  • 14,351
  • 6
  • 61
  • 98
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);
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • I think, this is almost the same idea as @Travesty3. I got the same NULL. – Vishwa Nov 22 '13 at 20:15
  • If you manuually remove that part from the string and run it though JSONLint or similar, to you get valid JSON? – Mike Brant Nov 22 '13 at 20:16
  • Yes, I tries that. It said, there should be quotes over fieldNames. Thats what I mentioned in the question earlier. – Vishwa Nov 22 '13 at 20:21
  • @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. – Mike Brant Nov 22 '13 at 20:28
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));
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • The `rtrim()` works gracefully even if there is no trailing `;`. Does the solution work for you? – hek2mgl Nov 22 '13 at 20:12
  • No, Its not working. I think, rtrim() has nothing to do here. Therefore,it is same as the one I tried already. – Vishwa Nov 22 '13 at 20:19
  • 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?... – hek2mgl Nov 23 '13 at 02:52
  • 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`. – Vishwa Nov 23 '13 at 03:10
  • Then I assume they don't want you leaking their data + you have no understanding about how to do it anyway... Good night – hek2mgl Nov 23 '13 at 03:13