last day (14 days later) » 

1:14 PM
1
A: JSON data to javascript Array Android browser .length gives wrong value

Jason CustI think you have a misunderstanding of what JSON is. JSON is a string and not an object hence it's abbreviation of JavaScript Object Notation. What you want is colloquially referred to as a POJO or Plain Old Javascript Object. They are different. It seems like whatever you are using to make the ...

 
when i try to parse i am getting error "Uncaught SyntaxError: Unexpected token o"
 
What does console.log(typeof json) show you?
 
it is showing as "object"
 
JSON.parse expects a string not an object. Is this is on the browsers getting the odd results? Did you post the entire returned object?
 
This is what i have on my JSON file [ { "url": "google.com" }, { "url": "yahoo.com" } ]
 
1:14 PM
But what is returned to the browser? Can you log out the entire object. Also note that is invalid JSON. You can't have semicolons where you have them.
If that is the object you are returning to the browser then you have a few issues since it is not valid JSON.
 
a valid representation would be '[ { "url": "google.com" }, { "url": "yahoo.com" } ]'
Okay, that is not JSON.
you need to convert that object to JSON on your server before you send it.
for instance on node.js you would do: JSON.stringify(obj);
That is why you are getting that error. The parser is hitting that first 0 and it sees that it's an invalid character.
 
Anything wrong here? pastebin.com/2QHMKsk2
 
No...as long as the response is JSON that should auto-parse it for you correctly.
 
:( OMG! but i am getting previous pastbin :( as result
 
1:29 PM
it seems like it's working fine...do this with your response: console.log(JSON.stringify(xhr.response))
it looks like you are getting the auto-parsed object from xhr
so that should work normal for you
 
it return : [{"url":"http://google.com"},{"url":"http://yahoo.com"}]
 
so that is working as expected
you should be able to do xhr.response[0].url
 
okay, so it works
if it's not working on the other browsers it's because they don't support responseType set to json
see this post about the browser support for specifying json as the responseType
so you can do two things, typecheck the response and if it's a string still that means the browser doesn't support the responseType setting so you will need to parse it manually
 
when i do this alert(xhr.response[0].url) working on Desktop and all major mobile browsers, on Default Android and UC Web it is "undefined" :(
 
1:35 PM
right
var obj = typeof xhr.response === 'string' ? JSON.parse(xhr.response) : xhr.response;
 
console.log(obj) getting same postimg.org/image/82g47chq9/c61d94af
 
right
that's what you want
that just checks to see if the response is a string and if so it will parse it to an object
 
alert(obj[0].url) get google.com on desktop and android , i think it is working fine now... but what was my mistake?
 
It's not your mistake it's the lack of support on certain browsers for responseType
 
ok thanks for helping me :) please give full answer on my question ,,may help others :)
 
1:52 PM
done.
don't forget to mark it as the accepted answer
(also as an alternative you can drop the responseType setting altogether and just manually parse all responses)
 
Yes :) i will
 
happy coding! :)
 
thanks :)
 

last day (14 days later) »