如何从javascript解析json? [英] How To parse json from javascript ?
本文介绍了如何从javascript解析json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好
是否可以将用户更改为json链接?
i想要将其更改为:users:http: //localhost/../.../..json
这里是一个例子:
Hello
is it possible to change the users into a json link ?
i want to change it become : users: http://localhost/../.../..json
here is the example :
$("#default-textarea").mention({
users: [{
name: 'Lindsay Made',
username: 'LindsayM',
image: 'http://placekitten.com/25/25'
}, {
name: 'Rob Dyrdek',
username: 'robdyrdek',
image: 'http://placekitten.com/25/24'
}, {
name: 'Rick Bahner',
username: 'RickyBahner',
image: 'http://placekitten.com/25/23'
}, {
name: 'Jacob Kelley',
username: 'jakiestfu',
image: 'http://placekitten.com/25/22'
}, {
name: 'John Doe',
username: 'HackMurphy',
image: 'http://placekitten.com/25/21'
}, {
name: 'Charlie Edmiston',
username: 'charlie',
image: 'http://placekitten.com/25/20'
}, {
name: 'Andrea Montoya',
username: 'andream',
image: 'http://placekitten.com/24/20'
}, {
name: 'Jenna Talbert',
username: 'calisunshine',
image: 'http://placekitten.com/23/20'
}, {
name: 'Street League',
username: 'streetleague',
image: 'http://placekitten.com/22/20'
}, {
name: 'Loud Mouth Burrito',
username: 'Loudmouthfoods',
image: 'http://placekitten.com/21/20'
}]
});
推荐答案
( #default-textarea)。提及({
users:[{
name:' Lindsay Made',
用户名:' LindsayM',
image:' http://placekitten.com/25/25'
},{
名称:' Rob Dyrdek',
用户名:' robdyrdek',
image: ' http://placekitten.com/25/24'
},{
name:' Rick Bahner',
用户名:' RickyBahner',
image :' http://placekitten.com/25/23'
},{
name:' Jacob Kelley',
用户名:' jakiestfu',
image:' http://placekitten.com/25/22'
},{
name:' John Doe',
用户名:' HackMurphy',
image:' http://placekitten.com/25/21'
},{
name:' Charlie Edmiston',
用户名:' charlie',
image:' http://placekitten.com/25/20'
},{
name:' Andrea Montoya',
用户名:' andream',
image:' http://placekitten.com/24/20'
},{
name:' Jenna Talbert',
用户名:' calisunshine',
image: 跨度> http://placekitten.com/23/20'
},{
name: ' 街头联赛',
用户名:' streetleague',
image:' http://placekitten.com/ 22/20'
},{
name:' Loud Mouth Burrito' ,
用户名:' Loudmouthfoods',
image:< span class =code-string>' http://placekitten.com/21/20'
}]
});
("#default-textarea").mention({ users: [{ name: 'Lindsay Made', username: 'LindsayM', image: 'http://placekitten.com/25/25' }, { name: 'Rob Dyrdek', username: 'robdyrdek', image: 'http://placekitten.com/25/24' }, { name: 'Rick Bahner', username: 'RickyBahner', image: 'http://placekitten.com/25/23' }, { name: 'Jacob Kelley', username: 'jakiestfu', image: 'http://placekitten.com/25/22' }, { name: 'John Doe', username: 'HackMurphy', image: 'http://placekitten.com/25/21' }, { name: 'Charlie Edmiston', username: 'charlie', image: 'http://placekitten.com/25/20' }, { name: 'Andrea Montoya', username: 'andream', image: 'http://placekitten.com/24/20' }, { name: 'Jenna Talbert', username: 'calisunshine', image: 'http://placekitten.com/23/20' }, { name: 'Street League', username: 'streetleague', image: 'http://placekitten.com/22/20' }, { name: 'Loud Mouth Burrito', username: 'Loudmouthfoods', image: 'http://placekitten.com/21/20' }] });
Although your question is not so clear but as your question Heading implies you want to convert your javascript array (users) to json.So I give you a solution.Happy coding. :)
<script type="text/javascript">
function JavascriptArrayToJSON() {
var users = [{
name: 'Lindsay Made',
username: 'LindsayM',
image: 'http://placekitten.com/25/25'
}, {
name: 'Rob Dyrdek',
username: 'robdyrdek',
image: 'http://placekitten.com/25/24'
}, {
name: 'Rick Bahner',
username: 'RickyBahner',
image: 'http://placekitten.com/25/23'
}, {
name: 'Jacob Kelley',
username: 'jakiestfu',
image: 'http://placekitten.com/25/22'
}, {
name: 'John Doe',
username: 'HackMurphy',
image: 'http://placekitten.com/25/21'
}, {
name: 'Charlie Edmiston',
username: 'charlie',
image: 'http://placekitten.com/25/20'
}, {
name: 'Andrea Montoya',
username: 'andream',
image: 'http://placekitten.com/24/20'
}, {
name: 'Jenna Talbert',
username: 'calisunshine',
image: 'http://placekitten.com/23/20'
}, {
name: 'Street League',
username: 'streetleague',
image: 'http://placekitten.com/22/20'
}, {
name: 'Loud Mouth Burrito',
username: 'Loudmouthfoods',
image: 'http://placekitten.com/21/20'
}]
var jsonResult;
var string_json;
string_json =JSON.stringify(users);
jsonResult = JSON.parse(string_json); //Here is your json
}
</script>
这篇关于如何从javascript解析json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文