最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can i get values from json and display them in javascript - Stack Overflow

programmeradmin1浏览0评论

I want to get json from a web page. The json on the page is in the format

[{"ID":"151032",
  "user":"UsersName",
  "message":"This is a message.",
  "date":"1293452007",
  "replies":"1",
  "categories":false,
  "categoriesArray":[],
  "lat":"0.000000000000000",
  "lng":"0.000000000000000"}] 

How can I get user, message, ID and replies from the JSON and display it on another webpage via javascript?

Example: hello UsersName your Id is: 151032 and your message is: This is a message. it has 1 replies.

note: there will be multiple sets of this i.e.

[{"ID":"151032",
  "user":"UsersName1",
  "message":"This is a message.",
  "date":"1293452007",
  "replies":"1",
  "categories":false,
  "categoriesArray":[],
  "lat":"0.000000000000000",
  "lng":"0.000000000000000"},
 {"ID":"151033",
  "user":"UsersName2",
  "message":"This is another message.",
  "date":"1293452007",
  "replies":"2",
  "categories":false,
  "categoriesArray":[],
  "lat":"0.000000000000000",
  "lng":"0.000000000000000"}]

I want to get json from a web page. The json on the page is in the format

[{"ID":"151032",
  "user":"UsersName",
  "message":"This is a message.",
  "date":"1293452007",
  "replies":"1",
  "categories":false,
  "categoriesArray":[],
  "lat":"0.000000000000000",
  "lng":"0.000000000000000"}] 

How can I get user, message, ID and replies from the JSON and display it on another webpage via javascript?

Example: hello UsersName your Id is: 151032 and your message is: This is a message. it has 1 replies.

note: there will be multiple sets of this i.e.

[{"ID":"151032",
  "user":"UsersName1",
  "message":"This is a message.",
  "date":"1293452007",
  "replies":"1",
  "categories":false,
  "categoriesArray":[],
  "lat":"0.000000000000000",
  "lng":"0.000000000000000"},
 {"ID":"151033",
  "user":"UsersName2",
  "message":"This is another message.",
  "date":"1293452007",
  "replies":"2",
  "categories":false,
  "categoriesArray":[],
  "lat":"0.000000000000000",
  "lng":"0.000000000000000"}]
Share Improve this question edited Dec 28, 2010 at 20:24 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Dec 28, 2010 at 20:17 user556396user556396 6073 gold badges14 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Use JSON.parse and then access the properties like normal object properties. E.g.

var msgs = JSON.parse(json);

for(var i = 0, l = msgs.length; i < l; i++) {
    var msg = msgs[i];
    var div = document.createElement('div');
    div.innerHTML = 'Hello ' + msg.user + ' your Id is: ' + msg.ID + 'and your message is: ' + msg.message + ' it has ' + msg.replies + ' replies';
    document.body.appendChild(div);
}

Working DEMO

Although it is not safe, you can eval() the JSON data (but this could be a security issue for your users, so it would be best to use a parsing library). After eval()ing it, you can access the data like data[0]["ID"]

You can get a JSON parser here: http://www.json/js.html

发布评论

评论列表(0)

  1. 暂无评论