在 Chrome 控制台中使用 jQuery 创建多维 json 数组
Creating multidimensional json array using jQuery in Chrome Console
我有一个可用的 jQuery 脚本,它将使用 Chrome 的控制台从网站输出所需的字段和值(是的,为简洁起见,它必须在控制台中完成)。
它工作正常,但由于返回的字段数量,如果它可以分解成多维数组,它会更容易阅读。
下面是来自控制台的 运行 的缩短 jQuery 脚本。这是一个受密码保护的网站,所以很遗憾我无法提供该网页,但我希望以下示例易于理解。
因为我是一个 jQuery 小人物,所以我不确定在脚本末尾尝试合并多个对象时会发生什么,例如 "arr.push(obj);"
这是带有示例输出值的工作脚本。
##工作脚本##
var arr = [];
jQuery('#BasicInfo > table > tbody > tr >').each(function (i, v)
{
var obj = {};
var $this = jQuery(this);
// section 1
obj.orgid = jQuery('#org').val();
obj.userid = jQuery('#userid').val();
obj.companyname = jQuery('#companyname').val();
// group 1
obj.groupname1 = jQuery('#group_1').val();
obj.productgroupid1 = jQuery('#prodgroup_1').val();
obj.usedflag1 = jQuery('#flag_1').val();
obj.webaddress1 = jQuery('#webaddress_1').val();
// group 2
obj.groupname2 = jQuery('#group_2').val();
obj.productgroupid2 = jQuery('#prodgroup_2').val();
obj.usedflag2 = jQuery('#flag_2').val();
obj.webaddress2 = jQuery('#webaddress_2').val();
arr.push(obj);
}); JSON.stringify(arr);
JSON输出
[{
"orgid":"40",
"userid":"BrennanHuff",
"companyname":"Prestige World Wide",
"groupname1":"Security Services",
"productgroupid1":"1",
"usedflag1":"Y",
"webaddress1":"www.santamaria.com",
"groupname2":"Investors ?",
"productgroupid2":"2",
"usedflag2":"Y",
"webaddress2":"www.PrestigeWorldWide_Wide_Wide_Wide.com"
}]
目标
然而,我想要完成的是学习一种使用 jQuery
创建更复杂 JSON 数组的方法
我不确定如何创建多个对象(?),然后在最后推出然后字符串化。我已经搜索过,但似乎找不到使用 jQuery 在控制台中完成此操作的方法,主要是因为我对术语还是陌生的。
感谢大家花时间阅读本文并提供见解和反馈 - 非常感谢。
期望的JSON输出
[
"companyinfo": [{
"orgid":"40",
"userid":"BrennanHuff",
"companyname":"Prestige World Wide"
}],
"group_1": [{
"groupname1":"Security Services",
"productgroupid1":"1",
"usedflag1":"Y",
"webaddress1":"www.santamaria.com"
}],
"group_2": [{
"groupname2":"Investors ?",
"productgroupid2":"2",
"usedflag2":"Y",
"webaddress2":"www.PrestigeWorldWide_Wide_Wide_Wide.com"
}]
]
var arr = []; // the array
var obj = {}; // the object
obj.companyinfo = [{}]; // create the header array
obj.group_1 = [{}]; // create the header array
obj.group_2 = [{}]; // create the header array
// add data
obj.companyinfo[0].orgid = "40";
obj.companyinfo[0].userid = "BrennanHuff";
obj.companyinfo[0].companyname = "Prestige World Wide";
obj.group_1[0].groupname1 = "Security Services";
obj.group_1[0].productgroup1 = "1";
obj.group_1[0].usedflag1 = "Y";
obj.group_1[0].webaddress1 = "www.santamaria.com";
// continue adding here...
// if you want to add another object to the group_1 array
// then just use obj.push again pointing where you want it
// to go for example.
// Delete this if you dont want it.
//-------------------
var otherObj = {};
otherObj.groupname1b = "groupname1 b";
otherObj.something = [{}];
obj.group_1.push(otherObj);
//-------------------
// push the obj into the array
arr.push(obj);
console.log(arr);
console.log("-------------------------");
// Security Services in the group_1 array
console.log(arr[0].group_1[0].groupname1);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
Take a look at how I did it an I hope this is what you are looking for.
我有一个可用的 jQuery 脚本,它将使用 Chrome 的控制台从网站输出所需的字段和值(是的,为简洁起见,它必须在控制台中完成)。
它工作正常,但由于返回的字段数量,如果它可以分解成多维数组,它会更容易阅读。
下面是来自控制台的 运行 的缩短 jQuery 脚本。这是一个受密码保护的网站,所以很遗憾我无法提供该网页,但我希望以下示例易于理解。
因为我是一个 jQuery 小人物,所以我不确定在脚本末尾尝试合并多个对象时会发生什么,例如 "arr.push(obj);"
这是带有示例输出值的工作脚本。
##工作脚本##
var arr = [];
jQuery('#BasicInfo > table > tbody > tr >').each(function (i, v)
{
var obj = {};
var $this = jQuery(this);
// section 1
obj.orgid = jQuery('#org').val();
obj.userid = jQuery('#userid').val();
obj.companyname = jQuery('#companyname').val();
// group 1
obj.groupname1 = jQuery('#group_1').val();
obj.productgroupid1 = jQuery('#prodgroup_1').val();
obj.usedflag1 = jQuery('#flag_1').val();
obj.webaddress1 = jQuery('#webaddress_1').val();
// group 2
obj.groupname2 = jQuery('#group_2').val();
obj.productgroupid2 = jQuery('#prodgroup_2').val();
obj.usedflag2 = jQuery('#flag_2').val();
obj.webaddress2 = jQuery('#webaddress_2').val();
arr.push(obj);
}); JSON.stringify(arr);
JSON输出
[{
"orgid":"40",
"userid":"BrennanHuff",
"companyname":"Prestige World Wide",
"groupname1":"Security Services",
"productgroupid1":"1",
"usedflag1":"Y",
"webaddress1":"www.santamaria.com",
"groupname2":"Investors ?",
"productgroupid2":"2",
"usedflag2":"Y",
"webaddress2":"www.PrestigeWorldWide_Wide_Wide_Wide.com"
}]
目标
然而,我想要完成的是学习一种使用 jQuery
创建更复杂 JSON 数组的方法我不确定如何创建多个对象(?),然后在最后推出然后字符串化。我已经搜索过,但似乎找不到使用 jQuery 在控制台中完成此操作的方法,主要是因为我对术语还是陌生的。
感谢大家花时间阅读本文并提供见解和反馈 - 非常感谢。
期望的JSON输出
[
"companyinfo": [{
"orgid":"40",
"userid":"BrennanHuff",
"companyname":"Prestige World Wide"
}],
"group_1": [{
"groupname1":"Security Services",
"productgroupid1":"1",
"usedflag1":"Y",
"webaddress1":"www.santamaria.com"
}],
"group_2": [{
"groupname2":"Investors ?",
"productgroupid2":"2",
"usedflag2":"Y",
"webaddress2":"www.PrestigeWorldWide_Wide_Wide_Wide.com"
}]
]
var arr = []; // the array
var obj = {}; // the object
obj.companyinfo = [{}]; // create the header array
obj.group_1 = [{}]; // create the header array
obj.group_2 = [{}]; // create the header array
// add data
obj.companyinfo[0].orgid = "40";
obj.companyinfo[0].userid = "BrennanHuff";
obj.companyinfo[0].companyname = "Prestige World Wide";
obj.group_1[0].groupname1 = "Security Services";
obj.group_1[0].productgroup1 = "1";
obj.group_1[0].usedflag1 = "Y";
obj.group_1[0].webaddress1 = "www.santamaria.com";
// continue adding here...
// if you want to add another object to the group_1 array
// then just use obj.push again pointing where you want it
// to go for example.
// Delete this if you dont want it.
//-------------------
var otherObj = {};
otherObj.groupname1b = "groupname1 b";
otherObj.something = [{}];
obj.group_1.push(otherObj);
//-------------------
// push the obj into the array
arr.push(obj);
console.log(arr);
console.log("-------------------------");
// Security Services in the group_1 array
console.log(arr[0].group_1[0].groupname1);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
Take a look at how I did it an I hope this is what you are looking for.