在jQuery中从JSON值创建字符串

时间:2013-02-04 15:22:32

标签: jquery json string google-books

我想从JSON对象创建一串ISBN来搜索Google图书中的多本图书。

我可以通过以下路径解析Google Books的JSON来获取ISBN:

volumeInfo.industryIdentifiers[0].identifier

(我在这里做一个简单的书单:jsfiddle.net/LyNfX

如何将每个值串在一起以获得此查询结构,其中每个ISBN前面都有“isbn:”,并且在第一个之后它们用“OR”分隔

https://www.google.com/search?btnG=Search+Books&tbm=bks&q=Springfield+isbn:1416549838+OR+isbn:068482535X+OR+isbn:0805093079+OR+isbn:0306810328

2 个答案:

答案 0 :(得分:3)

从一系列名为list的ISBN字符串开始:

list.map(function(v){return "isbn:"+v;}).join("+OR+")

至于构建您的ISBN列表,我理解为您identifier中的industryIdentifiers道具(如果industryIdentifier是真实的Array):

var list = [];
volumeInfo.industryIdentifiers.forEach(function(e,i){list.push(e.identifier);});

你也可以一举构建最终的字符串,而不是构建一个数组,但这意味着额外的逻辑,以防止插入额外的分隔符(+OR+作为分隔符)

var output = "";
volumeInfo.industryIdentifiers.forEach(function(e,i){output += "isbn:"+e.identifier+"+OR+";});
output.slice(0,-4); // clear out last +OR+

答案 1 :(得分:1)

var arrayOfISBNs = [123,456,789...];
var result = "isbn:" + arrayOfISBNs.join(" OR isbn:")

只需使用数组连接。