tags:

views:

164

answers:

2

(This may be a dumb question)

I am using a RESTful API to get data out of a database such as firstname, lastname etc. The API gives me the data in JSON and I want to use some of it in a webpage.

For example, I want to print

Welcome firstname lastname.

using the data from the JSON string.

How do I do this?

+1  A: 

JSON is object notation. Whatever language you're using, you should be able to find a JSON library that can convert back and forth between a string and the object.

If you're using JavaScript, this code should work. The description for usage is here.

Nosredna
A: 

Assuming "myJSONtext" is well formed JSON, you can get an object representing the data with an eval:

var myObject = eval('(' + myJSONtext + ')')

Jason Watts
Using eval is generally frowned upon for security reasons because executable code can get through.
Nosredna
The JSON is coming from your webservice - what malicious code is going to get into there? (assuming you are sanitizing inputs going into your database)
russau
The eval was assumed to be done against sanitized JSON, as mentioned by russau.
Jason Watts
As Douglas Crockford often points out, your server may be trusted, but not necessarily competent. Or it maybe be sending JSON along from somewhere else. It doesn't hurt to be safe at the business end of the JSON--where it can be executed.
Nosredna
The service provider is handing back the json response in the headers along with an oauth_token and oauth_token_secret. The json contains the person data. I'm storing the tokens in cookies but I want to take some of the json data and display it in the browser of the user. I've been using PHP. Is eval safe for this?
motboys