JQuery如何从JSON数组中获取完整数据

Posted

技术标签:

【中文标题】JQuery如何从JSON数组中获取完整数据【英文标题】:how to JQuery get full data from JSON array 【发布时间】:2019-12-23 09:52:34 【问题描述】:

尝试获取json数据但显示一条记录时如何解决

"d":["__type":"Tolu+mirex_tbl_amsfile","SITEID":"366","ARRIVALAIRPORT":"","WAYBILLORIGINATOR":"","AIRLINEPREFIX":"","AWBSERIALNUMBER":"","HOUSEAWB":"","MASTERAWBINDICATOR":"","ORIGINAIRPORT":"","PIECES":"1","WEIGHTCODE":"K","WEIGHT":"30,00","DESCRIPTION":"mouse","FDAINDICATOR":"","IMPORTINGCARRIER":"","FLIGHTNUMBER":"","ARRIVALDAY":"","ARRIVALMONTH":"","SHIPPERNAME":"TEST","SHIPPERSTREETADDRESS":"TEST","SHIPPERCITY":"Antalya","SHIPPERSTATEORPROVINCE":"Alanya","SHIPPERPOSTALCODE":"07400","SHIPPERCOUNTRY":"TR","SHIPPERTELEPHONE":"1","CONSIGNEENAME":"ARZU","CONSIGNEESTREETADDRESS":"LIMAN MAH","CONSIGNEECITY":"","CONSIGNEESTATEORPROVINCE":"","CONSIGNEEPOSTALCODE":"34100","CONSIGNEECOUNTRY":"TR","CONSIGNEETELEPHONE":"02124310707","AMENDMENTFLAG":"","AMENDMENTCODE":"21","AMENDMENTREASON":"","PTPDESTINATION":"","PTPDESTINATIONDAY":"","PTPDESTINATIONMONTH":"","BOARDEDPIECES":"","BOARDEDWEIGHTCODE":"","BORDEDWEIGHT":"","PARTIALSHIPMENTREF":"","BROKERCODE":"","INBONDDESTINATION":"","INBONDDESTINATIONTYPE":"","BONDEDCARRIERID":"","ONWARDCARRIER":"","BONDEDPREMISESID":"","TRANSFERCONTROLNUMBER":"","ENTRYTYPE":"","ENTRYNUMBER":"","COUNTRYOFORIGIN":"Afghanista","CUSTOMSVALUE":"25,00","CURRENCYCODE":"USD","HTSNUMBER":"","EXPREs-s-rELEASE":"Y","__type":"Tolu+mirex_tbl_amsfile","SITEID":"367","ARRIVALAIRPORT":"","WAYBILLORIGINATOR":"","AIRLINEPREFIX":"","AWBSERIALNUMBER":"","HOUSEAWB":"","MASTERAWBINDICATOR":"","ORIGINAIRPORT":"","PIECES":"1","WEIGHTCODE":"K","WEIGHT":"30,00","DESCRIPTION":"mouse","FDAINDICATOR":"","IMPORTINGCARRIER":"","FLIGHTNUMBER":"","ARRIVALDAY":"","ARRIVALMONTH":"","SHIPPERNAME":"TEST","SHIPPERSTREETADDRESS":"TEST","SHIPPERCITY":"Antalya","SHIPPERSTATEORPROVINCE":"Alanya","SHIPPERPOSTALCODE":"07400","SHIPPERCOUNTRY":"TR","SHIPPERTELEPHONE":"1","CONSIGNEENAME":"ARZU","CONSIGNEESTREETADDRESS":"LIMAN MAH","CONSIGNEECITY":"","CONSIGNEESTATEORPROVINCE":"","CONSIGNEEPOSTALCODE":"34100","CONSIGNEECOUNTRY":"TR","CONSIGNEETELEPHONE":"02124310707","AMENDMENTFLAG":"","AMENDMENTCODE":"21","AMENDMENTREASON":"","PTPDESTINATION":"","PTPDESTINATIONDAY":"","PTPDESTINATIONMONTH":"","BOARDEDPIECES":"","BOARDEDWEIGHTCODE":"","BORDEDWEIGHT":"","PARTIALSHIPMENTREF":"","BROKERCODE":"","INBONDDESTINATION":"","INBONDDESTINATIONTYPE":"","BONDEDCARRIERID":"","ONWARDCARRIER":"","BONDEDPREMISESID":"","TRANSFERCONTROLNUMBER":"","ENTRYTYPE":"","ENTRYNUMBER":"","COUNTRYOFORIGIN":"Afghanista","CUSTOMSVALUE":"25,00","CURRENCYCODE":"USD","HTSNUMBER":"","EXPREs-s-rELEASE":"Y","__type":"Tolu+mirex_tbl_amsfile","SITEID":"368","ARRIVALAIRPORT":"","WAYBILLORIGINATOR":"","AIRLINEPREFIX":"","AWBSERIALNUMBER":"","HOUSEAWB":"","MASTERAWBINDICATOR":"","ORIGINAIRPORT":"","PIECES":"1","WEIGHTCODE":"K","WEIGHT":"30,00","DESCRIPTION":"mouse","FDAINDICATOR":"","IMPORTINGCARRIER":"","FLIGHTNUMBER":"","ARRIVALDAY":"","ARRIVALMONTH":"","SHIPPERNAME":"TEST","SHIPPERSTREETADDRESS":"TEST","SHIPPERCITY":"Antalya","SHIPPERSTATEORPROVINCE":"Alanya","SHIPPERPOSTALCODE":"07400","SHIPPERCOUNTRY":"TR","SHIPPERTELEPHONE":"1","CONSIGNEENAME":"ARZU","CONSIGNEESTREETADDRESS":"LIMAN MAH","CONSIGNEECITY":"","CONSIGNEESTATEORPROVINCE":"","CONSIGNEEPOSTALCODE":"34100","CONSIGNEECOUNTRY":"TR","CONSIGNEETELEPHONE":"02124310707","AMENDMENTFLAG":"","AMENDMENTCODE":"21","AMENDMENTREASON":"","PTPDESTINATION":"","PTPDESTINATIONDAY":"","PTPDESTINATIONMONTH":"","BOARDEDPIECES":"","BOARDEDWEIGHTCODE":"","BORDEDWEIGHT":"","PARTIALSHIPMENTREF":"","BROKERCODE":"","INBONDDESTINATION":"","INBONDDESTINATIONTYPE":"","BONDEDCARRIERID":"","ONWARDCARRIER":"","BONDEDPREMISESID":"","TRANSFERCONTROLNUMBER":"","ENTRYTYPE":"","ENTRYNUMBER":"","COUNTRYOFORIGIN":"Afghanista","CUSTOMSVALUE":"25,00","CURRENCYCODE":"USD","HTSNUMBER":"","EXPREs-s-rELEASE":"Y","__type":"Tolu+mirex_tbl_amsfile","SITEID":"369","ARRIVALAIRPORT":"","WAYBILLORIGINATOR":"","AIRLINEPREFIX":"","AWBSERIALNUMBER":"","HOUSEAWB":"","MASTERAWBINDICATOR":"","ORIGINAIRPORT":"","PIECES":"1","WEIGHTCODE":"K","WEIGHT":"30,00","DESCRIPTION":"mouse","FDAINDICATOR":"","IMPORTINGCARRIER":"","FLIGHTNUMBER":"","ARRIVALDAY":"","ARRIVALMONTH":"","SHIPPERNAME":"","SHIPPERSTREETADDRESS":"","SHIPPERCITY":"","SHIPPERSTATEORPROVINCE":"","SHIPPERPOSTALCODE":"","SHIPPERCOUNTRY":"","SHIPPERTELEPHONE":"","CONSIGNEENAME":"ARZU","CONSIGNEESTREETADDRESS":"LIMAN MAH","CONSIGNEECITY":"","CONSIGNEESTATEORPROVINCE":"","CONSIGNEEPOSTALCODE":"34100","CONSIGNEECOUNTRY":"TR","CONSIGNEETELEPHONE":"02124310707","AMENDMENTFLAG":"","AMENDMENTCODE":"21","AMENDMENTREASON":"","PTPDESTINATION":"","PTPDESTINATIONDAY":"","PTPDESTINATIONMONTH":"","BOARDEDPIECES":"","BOARDEDWEIGHTCODE":"","BORDEDWEIGHT":"","PARTIALSHIPMENTREF":"","BROKERCODE":"","INBONDDESTINATION":"","INBONDDESTINATIONTYPE":"","BONDEDCARRIERID":"","ONWARDCARRIER":"","BONDEDPREMISESID":"","TRANSFERCONTROLNUMBER":"","ENTRYTYPE":"","ENTRYNUMBER":"","COUNTRYOFORIGIN":"Afghanista","CUSTOMSVALUE":"25,00","CURRENCYCODE":"USD","HTSNUMBER":"","EXPREs-s-rELEASE":"Y"]
            $.ajax(
                url: 'Tolu.asmx/ams_file',
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (response) 

                    $.each(response.d, function (key, value) 
                        dataArray = [
                            SITEID: value.SITEID,
                            ARRIVALAIRPORT: value.ARRIVALAIRPORT,
                            WAYBILLORIGINATOR: value.WAYBILLORIGINATOR,
                            AIRLINEPREFIX: value.AIRLINEPREFIX,
                            AWBSERIALNUMBER: value.AWBSERIALNUMBER,
                            HOUSEAWB: value.HOUSEAWB,
                            MASTERAWBINDICATOR: value.MASTERAWBINDICATOR,
                            ORIGINAIRPORT: value.ORIGINAIRPORT,
                            PIECES: value.PIECES,
                            WEIGHTCODE: value.WEIGHTCODE,
                            WEIGHT: value.WEIGHT,
                            DESCRIPTION: value.DESCRIPTION,
                            FDAINDICATOR: value.FDAINDICATOR,
                            IMPORTINGCARRIER: value.IMPORTINGCARRIER,
                            FLIGHTNUMBER: value.FLIGHTNUMBER,
                            ARRIVALDAY: value.ARRIVALDAY,
                            ARRIVALMONTH: value.ARRIVALMONTH,
                            SHIPPERNAME: value.SHIPPERNAME,
                            SHIPPERSTREETADDRESS: value.SHIPPERSTREETADDRESS,
                            SHIPPERCITY: value.SHIPPERCITY,
                            SHIPPERSTATEORPROVINCE: value.SHIPPERSTATEORPROVINCE,
                            SHIPPERPOSTALCODE: value.SHIPPERPOSTALCODE,
                            SHIPPERCOUNTRY: value.SHIPPERCOUNTRY,
                            SHIPPERTELEPHONE: value.SHIPPERTELEPHONE,
                            CONSIGNEENAME: value.CONSIGNEENAME,
                            CONSIGNEESTREETADDRESS: value.CONSIGNEESTREETADDRESS,
                            CONSIGNEECITY: value.CONSIGNEECITY,
                            CONSIGNEESTATEORPROVINCE: value.CONSIGNEESTATEORPROVINCE,
                            CONSIGNEEPOSTALCODE: value.CONSIGNEEPOSTALCODE,
                            CONSIGNEECOUNTRY: value.CONSIGNEECOUNTRY,
                            CONSIGNEETELEPHONE: value.CONSIGNEETELEPHONE,
                            AMENDMENTFLAG: value.AMENDMENTFLAG,
                            AMENDMENTCODE: value.AMENDMENTCODE,
                            AMENDMENTREASON: value.AMENDMENTREASON,
                            PTPDESTINATION: value.PTPDESTINATION,
                            PTPDESTINATIONDAY: value.PTPDESTINATIONDAY,
                            PTPDESTINATIONMONTH: value.PTPDESTINATIONMONTH,
                            BOARDEDPIECES: value.BOARDEDPIECES,
                            BOARDEDWEIGHTCODE: value.BOARDEDWEIGHTCODE,
                            BORDEDWEIGHT: value.BORDEDWEIGHT,
                            PARTIALSHIPMENTREF: value.PARTIALSHIPMENTREF,
                            BROKERCODE: value.BROKERCODE,
                            INBONDDESTINATION: value.INBONDDESTINATION,
                            INBONDDESTINATIONTYPE: value.INBONDDESTINATIONTYPE,
                            BONDEDCARRIERID: value.BONDEDCARRIERID,
                            ONWARDCARRIER: value.ONWARDCARRIER,
                            BONDEDPREMISESID: value.BONDEDPREMISESID,
                            TRANSFERCONTROLNUMBER: value.TRANSFERCONTROLNUMBER,
                            ENTRYTYPE: value.ENTRYTYPE,
                            ENTRYNUMBER: value.ENTRYNUMBER,
                            COUNTRYOFORIGIN: value.COUNTRYOFORIGIN,
                            CUSTOMSVALUE: value.CUSTOMSVALUE,
                            CURRENCYCODE: value.CURRENCYCODE,
                            HTSNUMBER: value.HTSNUMBER,
                            EXPREs-s-rELEASE: value.EXPREs-s-rELEASE
                        ];

                    )
                    $("#sonuc").html(JSON.stringify(dataArray));
                ,

                error: function (XMLHttpRequest, textStatus, errorThrown) 
                    alert(textStatus);
                

            )

我需要完整的记录数组

结果;

["SITEID":"369","ARRIVALAIRPORT":"","WAYBILLORIGINATOR":"","AIRLINEPREFIX":"","AWBSERIALNUMBER":"","HOUSEAWB":"","MASTERAWBINDICATOR":"","ORIGINAIRPORT":"","PIECES":"1","WEIGHTCODE":"K","WEIGHT":"30,00","DESCRIPTION":"mouse","FDAINDICATOR":"","IMPORTINGCARRIER":"","FLIGHTNUMBER":"","ARRIVALDAY":"","ARRIVALMONTH":"","SHIPPERNAME":"","SHIPPERSTREETADDRESS":"","SHIPPERCITY":"","SHIPPERSTATEORPROVINCE":"","SHIPPERPOSTALCODE":"","SHIPPERCOUNTRY":"","SHIPPERTELEPHONE":"","CONSIGNEENAME":"ARZU","CONSIGNEESTREETADDRESS":"LIMAN MAH","CONSIGNEECITY":"","CONSIGNEESTATEORPROVINCE":"","CONSIGNEEPOSTALCODE":"34100","CONSIGNEECOUNTRY":"TR","CONSIGNEETELEPHONE":"02124310707","AMENDMENTFLAG":"","AMENDMENTCODE":"21","AMENDMENTREASON":"","PTPDESTINATION":"","PTPDESTINATIONDAY":"","PTPDESTINATIONMONTH":"","BOARDEDPIECES":"","BOARDEDWEIGHTCODE":"","BORDEDWEIGHT":"","PARTIALSHIPMENTREF":"","BROKERCODE":"","INBONDDESTINATION":"","INBONDDESTINATIONTYPE":"","BONDEDCARRIERID":"","ONWARDCARRIER":"","BONDEDPREMISESID":"","TRANSFERCONTROLNUMBER":"","ENTRYTYPE":"","ENTRYNUMBER":"","COUNTRYOFORIGIN":"Afghanista","CUSTOMSVALUE":"25,00","CURRENCYCODE":"USD","HTSNUMBER":"","EXPREs-s-rELEASE":"Y"]

【问题讨论】:

…最小 - 使用尽可能少的代码,但仍会产生同样的问题。阅读how to create minimal reproducible example 【参考方案1】:

在处理响应时,您将替换循环中的dataArray。相反,将每条记录附加到dataArray。执行以下操作:

dataArray = []
$.each(response.d, function (key, value) 
            dataArray.push(
                SITEID: value.SITEID,
                ARRIVALAIRPORT: value.ARRIVALAIRPORT,
                ....
             );
             ....
 )

由于我无法执行你的代码,这里是简单的可运行代码进行说明。

你在做什么:

dataArray = []
response = [1, 2, 3, 4]
response.forEach(element => 
    dataArray=[x:element];
);
console.log(dataArray)

你想做什么:

dataArray = []
response = [1, 2, 3, 4]
response.forEach(element => 
    dataArray.push(x: element);
);
console.log(dataArray)

【讨论】:

@AliTolu 很高兴知道您的问题已解决。请不要忘记接受答案和/或投票:)

以上是关于JQuery如何从JSON数组中获取完整数据的主要内容,如果未能解决你的问题,请参考以下文章

用 Json 数组 MVC 4 填充 jquery 数据表

如何在运行时使用 jquery 创建 json 对象数组?

JQuery 从 JSON 数组中获取数据

PHP-AJAX:如何通过 php/json 数组从查询中填充 jquery 数据表

从数组php获取50条记录

如何使用 javascript / jQuery 获取数据 ajax api 数组?