简体   繁体   中英

How do you get and display json objects created by rails?

I've seen examples of getting arrays made into json but not pure variables. I'm new to json and need a ajax /javascript function that can print out the contents of something like this:

{"name":["1"],"email":["1"],"phone":["1"],"subject":["1"],"message":["1"]}

I created a jsfiddle.net that shows you how you could do this:

http://jsfiddle.net/6kLfk/

The javascript is this:

var data = {
"name": ["1"],
"email": ["1"],
"phone": ["1"],
"subject": ["1"],
"message": ["1"]
}

$.each(data, function(key, value) {
    $('#test').append(key + ": " + value + "<br />");
});

which outputs:

name: 1
email: 1
phone: 1
subject: 1
message: 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM