从jQuery中的JSON值创建字符串 [英] create string from JSON values in jQuery

查看:132
本文介绍了从jQuery中的JSON值创建字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JSON对象创建一个ISBN字符串,以搜索Google图书中的多本图书.

我可以通过使用以下路径解析Google图书的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

解决方案

从称为list的ISBN字符串数组开始:

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

关于建立您的ISBN列表,我理解是您的industryIdentifiers中的identifier道具(如果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+

I'd like to create a string of ISBNs from a JSON object to search multiple books in Google Books.

I can get the ISBNs by parsing Google Books' JSON with this path:

volumeInfo.industryIdentifiers[0].identifier

(Here I'm doing that to get a simple book list: jsfiddle.net/LyNfX )

How do I string together each value in order to get this query structure, where each ISBN is preceded by "isbn:", and after the first one they are separated by "OR"

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

解决方案

Starting from an array of ISBN strings called list:

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

As for building your list of ISBN's which I understand to be the identifier prop in your industryIdentifiers (if industryIdentifier is a bona fide Array):

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

You could also just build the final string in one fell swoop and not build an array at all, but this means extra logic to prevent inserting an extra delimiter (+OR+ being the delimiter)

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

这篇关于从jQuery中的JSON值创建字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆