• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Cant Parse JSON in Webview via Javascript

HotNoob

Lurker
Hi,

I am trying to parse some JSON Code in the webview of an android app; but it is completely failing.

Code:
window.android.log(base64_decode(emails[i]));
var test = JSON.parse(base64_decode(emails[i]));

and the results:

Code:
Javascript_LOG: {"subject":"cmU6IHJlcGx5IHRvIHlvdXIgInJvb21tYXRlIHdhbnRlZCEgbGFyZ2Ugcm9vbSBmb3IgcmVudCBpbiBzdywgNSBtaW4gZnJvbSBscnQiIGFkIG9uIGtpamlqaQ==","replyto":"","from":"ZXdib2JtYW4rY2FmXz1ob3Rub29iPXNlY2NvbW0ubmV0QGdtYWlsLmNvbQ==","date":"MTM3ODM0MzQwMA==","id":92}
E/Web Console: Uncaught SyntaxError: Unexpected token  at file:///android_asset/webView__emailList.html:1

I have tried using JSON2, and JQueries parseJSON function; both have returned the same error;
 
It looks to me as if only your values are base64 encoded and you are trying to base64 decode the whole string.

I think you would need to do the json parsing first and then base64 decode the values for each key.
 
That's a nice friendly reply...

Anyway. All I know, is I looked at the string in your log and tried the following php code on it:

[HIGH]
<?php
$json =
'{"subject":"cmU6IHJlcGx5IHRvIHlvdXIgInJvb21tYXRlIHdhbnRlZCEgbGFyZ2Ugcm9vbSBmb3IgcmVudCBpbiBzdywgNSBtaW4gZnJvbSBscnQiIGFkIG9uIGtpamlqaQ==","replyto":"","from":"ZXdib2JtYW4rY2FmXz1ob3Rub29iPXNlY2NvbW0ubmV0QGdtYWlsLmNvbQ==","date":"MTM3ODM0MzQwMA==","id":92}';
$fromjson = json_decode($json);

foreach ($fromjson as $key=>$value){
echo $key . ": " . base64_decode($value) . "\n";
}
?>[/HIGH]

Works perfectly for me:
 
That's a nice friendly reply...

Anyway. All I know, is I looked at the string in your log and tried the following php code on it:

[HIGH]
<?php
$json =
'{"subject":"cmU6IHJlcGx5IHRvIHlvdXIgInJvb21tYXRlIHdhbnRlZCEgbGFyZ2Ugcm9vbSBmb3IgcmVudCBpbiBzdywgNSBtaW4gZnJvbSBscnQiIGFkIG9uIGtpamlqaQ==","replyto":"","from":"ZXdib2JtYW4rY2FmXz1ob3Rub29iPXNlY2NvbW0ubmV0QGdtYWlsLmNvbQ==","date":"MTM3ODM0MzQwMA==","id":92}';
$fromjson = json_decode($json);

foreach ($fromjson as $key=>$value){
echo $key . ": " . base64_decode($value) . "\n";
}
?>[/HIGH]

Works perfectly for me:

well obviously im not going to find the answer here, with ******s like so.

2 posts in a row, absolutely completely MORONIC.
 
As for the issue, took a little rage to motivate me; The string had control characters in them, which was causing JSON to fail - i'm surprised that both json parsers had this issue, while the parser in JAVA handled it perfectly...

The solution:
Code:
window.android.log(base64_decode(emails[i]));
var test = JSON.parse(base64_decode(emails[i]).replace(/[\x00-\x1F\x7F]/g, ''));
 
Back
Top Bottom