我的JSON文件中有以下对象数组,我在解析时遇到问题:
{
"@name": "zlib License",
"component": [{
"name": "iCarousel",
"url": "http://github.com/nicklockwood/iCarousel/"
}, {
"name": "FXKeychain",
"url": "http://github.com/nicklockwood/FXKeychain/"
}, {
"name": "FXForms",
"url": "http://github.com/nicklockwood/FXForms/"
}],
"licensetext": " The zlib/libpng License Copyright (c) <year> <copyright holders> This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution."
}
使用这个jquery循环:
function convertJson(data) {
var jsondata = data;
var output = "<ul>";
$.each(jsondata.license, function(k, lic) {
$.each(lic.component, function(i, comp) {
output += "<li><b>Name: </b>" + comp.name + " <br /> <b>URL</b> : " + "<a href=' " + comp.url + " '>" + comp.url + "</a>" + " <br /> <b>License</b><br /><ul>" + "<div class='license'>" + lic.licensetext + "</div></ul><br />" + "</li>";
});
});
output += "</ul>";
document.getElementById("licenses").innerHTML = output;
};
convertJson(yourJson);
});
这是我当前的实现,但不确定如何获取内部对象(名称和网址在undefined
循环中显示$.each
的位置:jsfiddle
答案 0 :(得分:1)
更改JSON的结构,以便将单个组件包装在数组中或调整convertJson()
function convertJson(data) {
//...
$.each(jsondata.license, function(k, lic) {
var components = lic.component.length ? lic.component : [lic.component]; // <- this
$.each(components, function(i, comp) {
//...
});
});
//...
};
答案 1 :(得分:0)
组件元素有一些无效的Json - 一次是单个对象,另一个是对象数组。还有一些代码需要更正。 我把它们放在一起:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Licenses</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
var jsonData = {
"@name": "Reuters TV iOS",
"license": [{
"@name": "OpenSSL Combined License",
"component": [{
"name": "OpenSSL",
"url": "http://www.openssl.org/"
}],
"licensetext": "LICENSE ISSUES \r\n==============\r\n \r\nThe OpenSSL toolkit stays under a dual license, i.e. both the conditions of\r\nthe OpenSSL License and the original SSLeay license apply to the toolkit.\r\nSee below for the actual license texts. Actually both licenses are BSD-style\r\nOpen Source licenses. In case of any license issues related to OpenSSL\r\nplease contact openssl-core@openssl.org.\r\n OpenSSL License \r\n---------------\r\n \r\n==================================================================== Copyright (c) 1998-2008 The OpenSSL Project. All rights reserved. Redistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions\r\nare met:\r\n Redistributions of source code must retain the above copyright\r\n notice, this list of conditions and the following disclaimer.\r\n Redistributions in binary form must reproduce the above copyright\r\n notice, this list of conditions and the following disclaimer in\r\n the documentation and/or other materials provided with the\r\n distribution. All advertising materials mentioning features or use of this\r\n software must display the following acknowledgment: \r\n \"This product includes software developed by the OpenSSL Project\r\n for use in the OpenSSL Toolkit. (http://www.openssl.org/)\" The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\r\n endorse or promote products derived from this software without\r\n prior written permission. For written permission, please contact\r\n openssl-core@openssl.org. Products derived from this software may not be called \"OpenSSL\" nor may \"OpenSSL\" appear in their names without prior written permission of the OpenSSL Project. Redistributions of any form whatsoever must retain the following\r\n acknowledgment: \r\n \"This product includes software developed by the OpenSSL Project\r\n for use in the OpenSSL Toolkit (http://www.openssl.org/)\" \r\nTHIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT \"AS IS\" AND ANY\r\nEXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r\nPURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\r\nITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r\nNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r\nHOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r\nSTRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r\nARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\r\nOF THE POSSIBILITY OF SUCH DAMAGE. \r\n====================================================================\r\n \r\nThis product includes cryptographic software written by Eric Young\r\n(eay@cryptsoft.com). This product includes software written by Tim\r\nHudson (tjh@cryptsoft.com).\r\n Original SSLeay License \r\n -----------------------\r\n Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) \r\nAll rights reserved. \r\nThis package is an SSL implementation written\r\nby Eric Young (eay@cryptsoft.com). \r\nThe implementation was written so as to conform with Netscapes SSL.\r\n \r\nThis library is free for commercial and non-commercial use as long as\r\nthe following conditions are aheared to. The following conditions\r\napply to all code found in this distribution, be it the RC4, RSA,\r\nlhash, DES, etc., code; not just the SSL code. The SSL documentation\r\nincluded with this distribution is covered by the same copyright terms\r\nexcept that the holder is Tim Hudson (tjh@cryptsoft.com).\r\n\r\nCopyright remains Eric Young's, and as such any Copyright notices in\r\nthe code are not to be removed. \r\nIf this package is used in a product, Eric Young should be given attribution\r\nas the author of the parts of the library used. \r\nThis can be in the form of a textual message at program startup or\r\nin documentation (online or textual) provided with the package.\r\n Redistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions\r\nare met:\r\n Redistributions of source code must retain the copyright\r\n notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright\r\n notice, this list of conditions and the following disclaimer in the\r\n documentation and/or other materials provided with the distribution. All advertising materials mentioning features or use of this software\r\n must display the following acknowledgement: \r\n \"This product includes cryptographic software written by\r\n Eric Young (eay@cryptsoft.com)\" \r\n The word 'cryptographic' can be left out if the rouines from the library\r\n being used are not cryptographic related :-). If you include any Windows specific code (or a derivative thereof) from\r\n the apps directory (application code) you must include an acknowledgement:\r\n \"This product includes software written by Tim Hudson (tjh@cryptsoft.com)\"\r\n \r\nTHIS SOFTWARE IS PROVIDED BY ERIC YOUNG \"AS IS\" AND\r\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r\nARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\r\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r\nOR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r\nHOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r\nLIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r\nOUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r\nSUCH DAMAGE.\r\n \r\nThe licence and distribution terms for any publically available version or\r\nderivative of this code cannot be changed. i.e. this code cannot simply be\r\ncopied and put under another distribution licence\r\n[including the GNU Public Licence.]"
}, {
"@name": "SIL Open Font License 1.1",
"component": [{
"name": "Font-Awesome",
"url": "http://github.com/FortAwesome/Font-Awesome/"
}],
"licensetext": " SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 PREAMBLE The goals of the Open Font License (OFL) are to stimulate worldwide\r\ndevelopment of collaborative font projects, to support the font creation\r\nefforts of academic and linguistic communities, and to provide a free and\r\nopen framework in which fonts may be shared and improved in partnership\r\nwith others. The OFL allows the licensed fonts to be used, studied, modified and\r\nredistributed freely as long as they are not sold by themselves. The\r\nfonts, including any derivative works, can be bundled, embedded, \r\nredistributed and/or sold with any software provided that any reserved\r\nnames are not used by derivative works. The fonts and derivatives,\r\nhowever, cannot be released under any other type of license. The\r\nrequirement for fonts to remain under this license does not apply\r\nto any document created using the fonts or their derivatives. DEFINITIONS \"Font Software\" refers to the set of files released by the Copyright\r\nHolder(s) under this license and clearly marked as such. This may\r\ninclude source files, build scripts and documentation. \"Reserved Font Name\" refers to any names specified as such after the\r\ncopyright statement(s). \"Original Version\" refers to the collection of Font Software components as\r\ndistributed by the Copyright Holder(s). \"Modified Version\" refers to any derivative made by adding to, deleting,\r\nor substituting — in part or in whole — any of the components of the\r\nOriginal Version, by changing formats or by porting the Font Software to a\r\nnew environment. \"Author\" refers to any designer, engineer, programmer, technical\r\nwriter or other person who contributed to the Font Software. PERMISSION & CONDITIONS Permission is hereby granted, free of charge, to any person obtaining\r\na copy of the Font Software, to use, study, copy, merge, embed, modify,\r\nredistribute, and sell modified and unmodified copies of the Font\r\nSoftware , subject to the following conditions: 1) Neither the Font Software nor any of its individual components,\r\nin Original or Modified Versions, may be sold by itself. 2) Original or Modified Versions of the Font Software may be bundled,\r\nredistributed and/or sold with any software , provided that each copy\r\ncontains the above copyright notice and this license. These can be\r\nincluded either as stand-alone text files, human-readable headers or\r\nin the appropriate machine-readable metadata fields within text or\r\nbinary files as long as those fields can be easily viewed by the user. 3) No Modified Version of the Font Software may use the Reserved Font\r\nName(s) unless explicit written permission is granted by the corresponding\r\nCopyright Holder. This restriction only applies to the primary font name as\r\npresented to the users. 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font\r\nSoftware shall not be used to promote, endorse or advertise any\r\nModified Version, except to acknowledge the contribution(s) of the\r\nCopyright Holder(s) and the Author(s) or with their explicit written\r\npermission. 5) The Font Software, modified or unmodified, in part or in whole,\r\nmust be distributed entirely under this license, and must not be\r\ndistributed under any other license. The requirement for fonts to\r\nremain under this license does not apply to any document created\r\nusing the Font Software. TERMINATION This license becomes null and void if any of the above conditions are\r\nnot met. DISCLAIMER THE FONT SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\r\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF\r\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\r\nOF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE\r\nCOPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\r\nINCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL\r\nDAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\r\nFROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM\r\nOTHER DEALINGS IN THE FONT SOFTWARE. "
}, {
"@name": "zlib License",
"component": [{
"name": "iCarousel",
"url": "http://github.com/nicklockwood/iCarousel/"
}, {
"name": "FXKeychain",
"url": "http://github.com/nicklockwood/FXKeychain/"
}, {
"name": "FXForms",
"url": "http://github.com/nicklockwood/FXForms/"
}],
"licensetext": " The zlib/libpng License Copyright (c) <year> <copyright holders> This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution."
}],
"isProjectComplete": "true"
};
function convertJson(jsondata) {
var output = "<ul>";
$.each(jsondata.license, function(k, lic) {
output += '<li>';
$.each(lic.component, function(k, comp) {
output += "<b>Name: </b>" + comp.name + " <br /> <b>URL</b> : " + "<a href=' " + comp.url + " '>" + comp.url + "</a>" + " <br />";
});
output += "<b>License</b><br /><ul>" + "<div class='license'>" + lic.licensetext + "</div></ul><br />" + "</li>";
});
output += "</ul>";
$("#licenses").html(output);
};
$(document).ready( function() {
convertJson(jsonData);
});
</script>
</head>
<body>
<div id="licenses"></div>
</body>
</html>