Javascript 如何对字符串数据进行分类
我想对这个数据体、标题等进行分类Javascript 如何对字符串数据进行分类,javascript,string,Javascript,String,我想对这个数据体、标题等进行分类 { "offset": "0", "results": [ { "body": "A novelist, essayist, historian and critic, Geoff Dyer writes about whatever happens to interest him. ''I am the opposite of an expert,'' he said in an e-mail messa
{
"offset": "0",
"results": [
{
"body": "A novelist, essayist, historian and critic, Geoff Dyer writes about whatever happens to interest him. ''I am the opposite of an expert,'' he said in an e-mail message, ''a case study in how not to go about building a career as a writer.'' It's been quite a non-career. ''Yoga for People Who Can't Be Bothered to Do It'' won the W. H. Smith Best",
"byline": "By THE EDITORS",
"date": "20071028",
"title": "Up Front",
"url": "http://query.nytimes.com/gst/fullpage.html?res=9E0CE4D61130F93BA15753C1A9619C8B63"
},
{
"body": "PRINCETON, N.J. Many of us have known this scholar: The hair is well-streaked with gray, the chin has begun to sag, but still our tortured friend slaves away at a masterwork intended to change the course of civilization that everyone else just hopes will finally get a career under way. We even have a name for this sometimes pitied species -- the",
"byline": "By JOSEPH BERGER",
"date": "20071003",
"title": "ON EDUCATION; Exploring Ways to Shorten the Ascent to a Ph.D.",
"url": "http://www.nytimes.com/2007/10/03/education/03education.html"
}
]
}
结果
body == {"A novelist, essayist, historian and critic, Geoff Dyer writes about whatever happens to interest him. ''I am the opposite of an expert,'' he said in an e-mail message, ''a case study in how not to go about building a career as a writer.'' It's been quite a non-career. ''Yoga for People Who Can't Be Bothered to Do It'' won the W. H. Smith Best",
"byline": "By THE EDITORS", PRINCETON, N.J. Many of us have known this scholar: The hair is well-streaked with gray, the chin has begun to sag, but still our tortured friend slaves away at a masterwork intended to change the course of civilization that everyone else just hopes will finally get a career under way. We even have a name for this sometimes pitied species -- the"}
byline == ["By THE EDITORS", "By JOSEPH BERGER",}
data == ["20071028", "20071003"]
您必须遍历数据并执行您需要的操作(假设它存储在名为
data
的var中)
for(var i=0;i
var data=JSON.parse(JSON);
var输出={};
对于(var i=0,l=data.results.length;i
OP,你说的分类是什么意思?我想把这些数据分为正文、日期、标题、url……你能给我们看一个你想要的结果的例子吗?你需要使用JSON.parse(data)
,然后你可以用它做你想做的事情;最有可能的是循环查看数据。结果
并获取所需信息。
for (var i = 0; i < data.results.length; i++) {
console.log(data.results[i].body); //Or whatever you need
..
..
..
}
var data = JSON.parse(json);
var output = {};
for (var i = 0, l = data.results.length; i < l; i++) {
var el = data.results[i];
for (var p in el) {
if (el.hasOwnProperty(p)) {
if (!output[p]) { output[p] = []; }
output[p].push(el[p]);
}
}
}
console.log(output);