在发布到API

时间:2016-11-17 16:39:48

标签: javascript c# asp.net arrays

我有一个数组:

[{"IDAlokasiEmiten":154,"IDOrganization":12,"NamaOrganization":null,"CodeEmiten":"ANTM","NameEmiten":"AAneka Tambang (Persero) Tbk"},{"IDAlokasiEmiten":0,"IDOrganization":null,"NamaOrganization":null,"CodeEmiten":"ADHI","NameEmiten":"Adhi Karya (Persero) Tbk"}]

如何在发布到API之前更改某些值?

我希望POST到API:

[{"IDAlokasiEmiten":0,"IDOrganization":12,"NamaOrganization":null,"CodeEmiten":"ANTM","NameEmiten":"AAneka Tambang (Persero) Tbk"},{"IDAlokasiEmiten":0,"IDOrganization":12,"NamaOrganization":null,"CodeEmiten":"ADHI","NameEmiten":"Adhi Karya (Persero) Tbk"}]

这是我的AngularJS:

// GET Detail Alokasi Emiten
    $scope.emit.detailDaftarEmiten = [];
    $scope.addItemAlokasi = function () {
        $scope.emit.detailDaftarEmiten.push({
            IDAlokasiEmiten: 0,
            IDOrganization: 12,
            NamaOrganization: '',
            CodeEmiten: $scope.emit.detailDaftarEmiten.CodeEmiten,
            NameEmiten: $scope.emit.detailDaftarEmiten.NameEmiten
        });
        $scope.emit.detailDaftarEmiten.IDAlokasiEmiten = '';
        $scope.emit.detailDaftarEmiten.IDOrganization = '';
        $scope.emit.detailDaftarEmiten.NamaOrganization = '';
        $scope.emit.detailDaftarEmiten.CodeEmiten = '';
        $scope.emit.detailDaftarEmiten.NameEmiten = ''
    };
    $scope.resetForm = function () {
        $scope.emit.detailDaftarEmiten.IDAlokasiEmiten = ''; 
        $scope.emit.detailDaftarEmiten.IDOrganization = '';
        $scope.emit.detailDaftarEmiten.NamaOrganization = '';
        $scope.emit.detailDaftarEmiten.CodeEmiten = '';
        $scope.emit.detailDaftarEmiten.NameEmiten = ''
    };
    $scope.deleteItem = function (index) {
        $scope.emit.detailDaftarEmiten.splice(index, 1);
    };
    $scope.getTotalItems = function () {
        return $scope.detailDaftarEmiten.length;
    };

但它不起作用:(

我想为(IDAlokasiEmiten=0IDOrganization=12)设置所有值数组。

5 个答案:

答案 0 :(得分:0)

var obj=your code;
obj[0]["IDOrganisation"]=12;
//0 -> first object in array

这是简单的javascript。您应该访问MDN并了解更多信息......

答案 1 :(得分:0)

是的,你可以使用普通的javascript而不使用forEach方法的库来做到这一点。

forEach

arr=[{"IDAlokasiEmiten":154,"IDOrganization":12,"NamaOrganization":null,"CodeEmiten":"ANTM","NameEmiten":"AAneka Tambang (Persero) Tbk"},{"IDAlokasiEmiten":0,"IDOrganization":null,"NamaOrganization":null,"CodeEmiten":"ADHI","NameEmiten":"Adhi Karya (Persero) Tbk"}]
arr.forEach(function(a){
  a["IDAlokasiEmiten"]=0;
  a["IDOrganization"]=12;
});
console.log(arr);

答案 2 :(得分:0)

您可以尝试以下声明。

var arr = [{"IDAlokasiEmiten":154,"IDOrganization":12,"NamaOrganization":null,"CodeEmiten":"ANTM","NameEmiten":"AAneka Tambang (Persero) Tbk"},{"IDAlokasiEmiten":0,"IDOrganization":null,"NamaOrganization":null,"CodeEmiten":"ADHI","NameEmiten":"Adhi Karya (Persero) Tbk"}];
arr.map(i=>{i.IDAlokasiEmiten=0;i.IDOrganization=12;})

答案 3 :(得分:0)

var a = [{"IDAlokasiEmiten":154,"IDOrganization":12,"NamaOrganization":null,"CodeEmiten":"ANTM","NameEmiten":"AAneka Tambang (Persero) Tbk"},{"IDAlokasiEmiten":0,"IDOrganization":null,"NamaOrganization":null,"CodeEmiten":"ADHI","NameEmiten":"Adhi Karya (Persero) Tbk"}];

a.forEach(function(e) {
  e.IDAlokasiEmiten = 0;
  e.IDOrganization = 12;
});

JSON.stringify(a);

答案 4 :(得分:0)

首先,它的json数据。

您必须首先反序列化,然后序列化回json。

maven-antrun-plugin

使用NewtonSoft.Json dll。

您可以将此json反序列化为public class Sample { public int IDAlokasiEmiten { get; set; } public int? IDOrganization { get; set; } public object NamaOrganization { get; set; } public string CodeEmiten { get; set; } public string NameEmiten { get; set; } } ,然后遍历它并设置每个对象的属性。

<强> JSON

SampleClass[]

C#代码:

[{
    "IDAlokasiEmiten": 154,
    "IDOrganization": 12,
    "NamaOrganization": null,
    "CodeEmiten": "ANTM",
    "NameEmiten": "AAneka Tambang (Persero) Tbk"
}, {
    "IDAlokasiEmiten": 0,
    "IDOrganization": null,
    "NamaOrganization": null,
    "CodeEmiten": "ADHI",
    "NameEmiten": "Adhi Karya (Persero) Tbk"
}]