Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用asp mvc发布嵌套JSON正文_Javascript_C#_Json_Asp.net Mvc_Rest - Fatal编程技术网

Javascript 使用asp mvc发布嵌套JSON正文

Javascript 使用asp mvc发布嵌套JSON正文,javascript,c#,json,asp.net-mvc,rest,Javascript,C#,Json,Asp.net Mvc,Rest,我试图将一个嵌套的json主体发布到api中,我尝试了几件事,但都没有成功。另一件事是Json值来自不同的模型。这是一个有点复杂,我正在努力与它,任何形式的帮助将不胜感激 public Object getfreight() { var json = "{" + "'from': {" + "'postcode': '3207'" + "}," + "'to': {" + "'postcode': '2001'" + "}," + "'items': [

我试图将一个嵌套的json主体发布到api中,我尝试了几件事,但都没有成功。另一件事是Json值来自不同的模型。这是一个有点复杂,我正在努力与它,任何形式的帮助将不胜感激

public Object getfreight()
    {
         var json = "{" +
"'from': {" +
    "'postcode': '3207'" +
"}," +
"'to': {" +
    "'postcode': '2001'" +
"}," +
"'items': [" +
    "{" +
        "'length': 5," +
        "'height': 5," +
        "'width': 5," +
        "'weight': 5," +
        "'item_reference' : 'abc xyz'," +
        "'features':{" +
            "'TRANSIT_COVER': {" +
                "'attributes': {" +
                    "'cover_amount':1000" +
                "}" +
            "}" +
        "}" +
    "}" +
"]" +
"}";

 string url = 
"https://digitalapi.auspost.com.au/postage/parcel/domestic/calculate.json?length=22&to_postcode=3083&from_postcode=3076&weight=2&height=40&width=50&service_code=AUS_PARCEL_REGULAR";


         //synchronous client.
        var client = new WebClient();
        var serializer = new JavaScriptSerializer();
        client.Credentials = new NetworkCredential("***", "***"); 

        client.Headers.Add("Content-Type: application/json");
        client.Headers.Add("Accept: application/json");
        client.Headers.Add("account-number: ***");
        var data = serializer.Serialize(json);
        var content = client.UploadString(url,json);

        var jsonContent = serializer.Deserialize<Object>(content);
        return jsonContent;
    }
public对象get货运()
{
var json=“{”+
“'from':{”+
“‘邮政编码’:‘3207’”+
"}," +
“'to':{”+
“‘邮政编码’:‘2001’”+
"}," +
“‘项目’:[”+
"{" +
“长度”:5,”+
“‘高度’:5,”+
“宽度”:5,”+
“‘重量’:5,”+
“‘项目参考’:‘abc xyz’,”+
“'features':{”+
“‘运输保险’:{”+
“'attributes':{”+
“‘保险金额’:1000”+
"}" +
"}" +
"}" +
"}" +
"]" +
"}";
字符串url=
"https://digitalapi.auspost.com.au/postage/parcel/domestic/calculate.json?length=22&to_postcode=3083&from_postcode=3076&weight=2&height=40&width=50&service_code=AUS_PARCEL_REGULAR";
//同步客户端。
var client=new WebClient();
var serializer=新的JavaScriptSerializer();
client.Credentials=newnetworkcredential(“***”和“***”);
Add(“内容类型:application/json”);
client.Headers.Add(“Accept:application/json”);
client.Headers.Add(“账号:**”);
var data=serializer.Serialize(json);
var content=client.UploadString(url,json);
var jsonContent=serializer.Deserialize(内容);
返回jsonContent;
}

不能使用jQuery将json对象传递给API吗

 function Search () {
       try {
                    var req = {
                        length:"5",
                        Higet: "10",
                        item_reference: "",  
                    }

                    $.ajax({
                        url: https://digitalapi.auspost.com.au/postage/parcel/domestic,
                        type: 'POST',
                        data: req,
                        dataType: "json",
                        async: true,
                        success: function (result) { 


                        },
                        error: function (e) {

                        }
                    });
                }
                catch (e) {

                }
            }

你说的这个“嵌套json体”是什么?你有json样本吗?嗨,我已经编辑了代码,现在请查看json数据。你有这行
var data=serializer.Serialize(JsonData)
但是您没有在任何地方使用
数据
变量是的,因为JsonData还无效,它给出了错误,在修复之后,我必须将其添加为正文。如果我没有错,我可以使用WebClient()添加正文!