任何人都可以帮助我们,我们已经有很长时间了。
基本上,我们希望通过使用 JSON DATA 2(PODSUB_ID)数据与 JSON数据1(ID)动态合并2个JSON响应。请参阅下面的示例JSON数据。
JSON数据1
[
{
“启用”:1,
“ id”:1,
“ parent_screen”:“产品”,
“ category_title”:“常规储蓄帐户”,
“ category_desc”:“常规储蓄帐户”,
"category_imgpath": "wdbwebsite/Products/RegSavings/Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-ggggg.jpg",
"category_imgpath_filename": "Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-ggggg.jpg",
“功能”:“低维持余额\ ninterest在任何财富分支机构方便地\ ntransact方便,即使在周末没有分支服务费”,
“需求”:“ wdbwebsite/products/regsavings/products-regular_savings_account-requirent-products-regular_savings_account-requirlement-requirement-requirement-requirement-dtr0215.pdf”,
“需求_fileName”:“ products-regular_savings_account-requirement-products-regular_savings_account-requirent-requirlement-dtr0215.pdf”,
“ contactus_desc”:null,
“ retval”:1,
“ errmsg”:“找到的数据”
},,
{
“启用”:null,
“ id”:11,
“ parent_screen”:“产品”,
“ category_title”:“特殊储蓄帐户”,
“ category_desc”:“特殊储蓄帐户”,
“ category_imgpath”:“ wdbwebsite/products/specialsavings/products-savings_savings_account-img-2.jpg”,
“ category_imgpath_filename”:“ products-special_savings_account-img-2.jpg”,
“功能”:“在安置/任期\ noption成熟时,应记住利息,以使安置或充值倒计时”,
“需求”:“ wdbwebsite/products/specialsavings/products-savings_account-requart-requirement-products-regular_savings_account-requirlement-requirement-requirement-dtr0215.pdf”,
“需求_fileName”:“ products-special_savings_account-requirement-products-regular_savings_account-requirent-dtr0215.pdf”,
“ contactus_desc”:null,
“ retval”:1,
“ errmsg”:“找到的数据”
}
]
JSON数据2
[
{
prodsub_id:11,
parent_screen:“产品”,
narra_title:'nara title',
narra_content:'nara content',
优惠:'upers1'
},,
{
prodsub_id:11,
parent_screen:“产品”,
narra_title:'nara title',
narra_content:'nara content',
优惠:'upers2'
},,
{
prodsub_id:11,
parent_screen:“产品”,
narra_title:'nara title',
narra_content:'nara content',
优惠:null
},,
{
prodsub_id:10,
parent_screen:“产品”,
narra_title:“奈良标题”,
narra_content:'nara content',
优惠:null
}
]
因此,全部简化了这一切。
我们需要有此输出。
JSON最终输出
{
“ parent_screen”:“产品”,
“ catpory_imgpath”:“”,
“叙述”: [
{
“标题”:“ qwe”,
“ content”:“ qwe”
},,
{
“标题”:“ W”,
“ content”:“ qwer”
},,
{
“标题”:“ qwer”,
“ content”:“ asdf”
}
],,
“ category_title”:“常规储蓄帐户”,
“ category_desc”:“ qwer”,
“提供”: [
“ qwer”,
“ wqer”,
“ Qwerwe”
],,
“要求”: ””,
“ req_img”:“”,
“链接”:“普通萨维奇符号”,
“功能”:“ ASDF”,
“启用”:1,
“ id”:11,
“ img”:“”
}
由于第二个JSON数据具有 prodsub_id ,该存在于第一个JSON数据(id),
我们想将一些第二个JSON数据插入第一个JSON数据中,如 JSON最终输出所示。
对不起,我的英语不好,任何帮助都将不胜感激。
Can anyone help us, we have been on this for ages.
Basically, we want to merge 2 JSON responses dynamically by using JSON Data 2( prodsub_id) data with JSON Data 1(id). Please see below sample JSON data.
JSON Data 1
[
{
"enabled": 1,
"id": 1,
"parent_screen": "Products",
"category_title": "Regular Savings Account",
"category_desc": "Regular Savings Account",
"category_imgpath": "wdbwebsite/Products/RegSavings/Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-ggggg.jpg",
"category_imgpath_filename": "Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-ggggg.jpg",
"features": "Low maintaining balance\nInterest credited\nTransact conveniently at any WealthBank branch even on weekends with no Interbranch Service Fee",
"requirement": "wdbwebsite/Products/RegSavings/Products-Regular_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf",
"requirement_filename": "Products-Regular_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf",
"contactus_desc": null,
"retVal": 1,
"errmsg": "Data found"
},
{
"enabled": null,
"id": 11,
"parent_screen": "Products",
"category_title": "Special Savings Account",
"category_desc": "Special Savings Account",
"category_imgpath": "wdbwebsite/Products/SpecialSavings/Products-Special_Savings_Account-img-2.jpg",
"category_imgpath_filename": "Products-Special_Savings_Account-img-2.jpg",
"features": "Interest shall be credited upon maturity of the placement/term\nOption to roll-over the placement or top-up",
"requirement": "wdbwebsite/Products/SpecialSavings/Products-Special_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf",
"requirement_filename": "Products-Special_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf",
"contactus_desc": null,
"retVal": 1,
"errmsg": "Data found"
}
]
JSON DATA 2
[
{
prodsub_id: 11,
parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content',
offer: 'offer1'
},
{
prodsub_id: 11,
parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content',
offer: 'offer2'
},
{
prodsub_id: 11,
parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content',
offer: null
},
{
prodsub_id: 10,
parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content',
offer: null
}
]
So to simplify it all.
We need to have this output.
JSON FINAL OUTPUT
{
"parent_screen": "Products",
"category_imgpath": "",
"narrative": [
{
"title": "qwe",
"content": "qwe"
},
{
"title": "w",
"content": "qwer"
},
{
"title": "qwer",
"content": "asdf"
}
],
"category_title": "Regular Savings Account",
"category_desc": "qwer",
"offer": [
"qwer",
"wqer",
"qwerwe"
],
"requirement": "",
"req_img": "",
"link": "regularsavingsaccount",
"features": "asdf",
"enabled": 1,
"id": 11,
"img": ""
}
Since the 2nd JSON data has a prodsub_id that is existing in the 1st JSON data (id),
We would like to insert some 2nd JSON data into the 1st JSON data, as shown in JSON FINAL OUTPUT.
Sorry for my bad English, any help will be greatly appreciated.
发布评论
评论(2)
我认为做到这一点的最简单方法(可能不是最好的),是循环浏览JSON 1中的项目,然后在JSON 2上进行过滤器以返回使用Prodsub_id等于当前项目的ID的任何项目从JSON 1,然后只需添加JSON 2(如果存在)中的匹配项中所需的任何字段到JSON 1中的当前项目。
类似:
EDIT:如果有超过1个匹配(
匹配将始终是一个数组),如果
>循环添加匹配的每个项目,则需要添加中的逻辑,并将您需要的任何内容添加到
iteg> item
。参见有关过滤器功能的信息。
The easiest way I can think to do this (which may not be the best), would be to loop over the items in JSON 1 and then do a filter on JSON 2 to return any items with prodsub_id equal to the id of the current item from JSON 1 and then just add whatever fields you need from the matching item in JSON 2 (if one exists) to the current item from JSON 1.
Something like:
Edit: If there is more than 1 match (
matched
will always be an array), you'll need to add logic inside theif
to loop over each item that was matched and add whatever you need toitem
.See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter for info about the filter function.
为了实现 JSON最终输出,
我喜欢使用array.map&数组过滤器。请参阅下面。
简短的分步说明:
映射JSON1数据并在其中,为JSON2创建过滤器(narra_title,narra_content)。
映射创建的 narra_title filter(n_title),并在内部推动 narra_content 在临时数组支架上过滤数据(tempdata);
>
在映射数组(JSON1)
中分配新数据( tempdata )
这样: json1 [index] .narrative = tempdata;
最后,将tempdata设置回 tempdata = [] 或空数组
To achieve the JSON FINAL OUTPUT,
I did it like so, using array.map & array filter. Please see below.
A brief step-by-step explanation:
Mapping the json1 data and inside it, create a filter for json2(narra_title, narra_content).
Mapping the the created narra_title filter(n_title) and inside it pushes the narra_content filtered data on a temporary array holder(tempdata);
Assigning the new data ( tempdata ) inside the mapped array (json1)
like this : json1[index].narrative=tempdata;
and lastly,setting tempdata back to tempdata=[] or empty array