2016-08-18 57 views
0

阵列我有一个ASP.NET的API,它需要一个字符串模式:的Javascript:内对象

[HttpPost] 
public ActionResult Add(string model) 
{ 

    var m = JsonConvert.DeserializeObject<CustomModel>(model); 

    ... 

} 

到目前为止,我一直在做这将数据传递给它:

var addModel = { 
    "SomeValue": { 
     "Some": "example", 
     "Value": "example" 
    }, 
    "AnotherValue": "example" 
} 

var model = JSON.stringify(addModel); 

而且它工作得很好。但现在我需要出货数据是这样的:

var addModel = { 
    "SomeValue": { 
     "Some": "example", 
     "Value": "example" 
    }, 
    "AnotherValue": "example", 

    "MyArray[0].SomeValue": 1, 
    "MyArray[0].AnotherValue": a, 
    "MyArray[1].SomeValue": 1, 
    "MyArray[1].AnotherValue": a, 
} 

如何添加的对象,因此它可以被传递到后端以正确的格式?

+0

我想我不明白你的意思......'{“key”:[]}'这是将数组添加到对象的方式 – messerbill

+0

您也可以将数组推入数组'addModel'声明后addModel.MyArray.push({“SomeValue”:1,“AnotherValue”:a})' –

回答

0

数组你可以把它们直接与

addModel.MyArray[0] = { "SomeValue" : 1, "AnotherValue": a }; 

你可以推到阵列

addModel.MyArray.push({ "SomeValue" : 1, "AnotherValue": a }); 

addModel宣布

+0

谢谢!这工作得很好 –

2

就宣布它作为像这样

var addModel = { 
    "SomeValue": { 
     "Some": "example", 
     "Value": "example" 
    }, 
    "AnotherValue": "example",  
    "MyArray": [ 
     { "SomeValue" : 1, "AnotherValue": a }, 
     { "SomeValue" : 1, "AnotherValue": a } 
    ] 
} 
1
"MyArray": [ 
{ 
    "SomeValue": 1, 
    "AnotherValue": a 
}, 
{ 
    "SomeValue": 1, 
    "AnotherValue": a 
} 
] 
0
var myArray = [ 
    { 
     "a":1, 
     "b":2 
    }, 
    { 
     "a":1, 
     "b":2 
    } 
]; 

var addModel = { 
    "SomeValue": { 
     "Some": "example", 
     "Value": "example" 
    }, 
    "AnotherValue": "example", 
    "myArray": myArray 
};