使用asp mvc POST嵌套的JSON Body

时间:2017-11-20 03:10:22

标签: javascript c# json asp.net-mvc rest

我试图将一个嵌套的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;
    }

1 个答案:

答案 0 :(得分:0)

不能使用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) {

                }
            }