如何为嵌套对象创建Dictionary <string,string>

时间:2017-08-13 12:00:38

标签: c# ajax asp.net-web-api

我正在通过POST检索以下JSON到API

{
    "State":"Andhra_Pradesh",
    "District":"Guntur", 
    "Fact":"SELECT", 
    "Description":"",
    "FactDate":"",
    "FactNumber":"",
    "FactType":"SELECT",
    "Fact":{"Id":"1"}
}

我可以通过javascript执行Ajax请求,但我也想通过C#代码使用API​​。

我使用下面的代码,但我不太确定如何添加Fact对象?

var values = new Dictionary<string, string>
            {
                { "State", selectedState },
                { "District", selectedDistrict },
                { "Fact", ""},
                { "FactType", ""},
                { "FactNumber", ""},
                { "Description", ""},
                {"Fact", "{Id,1}" },
                {"FactDate", factDate.Date.ToString() }
            };

using (var httpClient = new HttpClient())
                { 
var content = new FormUrlEncodedContent(values); 
var response = await httpClient.PostAsync("http://api.in/" + "test", content);
}

如何将Fact对象添加到Dictionary?

4 个答案:

答案 0 :(得分:4)

在使用httpclient之前,您可能需要将要发送的数据定义为实际类。

如果您只有名称值对,那么您可以使用NameValueCollection并以formurlencoded形式发送,但由于您有复杂的类型,您可以在下面考虑这一点。

见下文。

public class Rootobject
{
    public string State { get; set; }
    public string District { get; set; }
    public Fact Fact { get; set; }
    public string Description { get; set; }
    public string CaseDate { get; set; }
    public string FactNumber { get; set; }
    public string FactType { get; set; }
}

public class Fact
{
    public string Id { get; set; }
}

用法如下。请务必添加对System.Net.Http.Formatting.dll

的引用
var client = new HttpClient();

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


    var model = new Rootobject { State = "Andhra_Pradesh", District = "Guntur", FactType = "SELECT", Description = "", CaseDate = "", FactNumber = "", Fact = new Fact { Id = "1"} };

    var data = await client.PostAsJsonAsync("http://api.in/" + "test", model);

答案 1 :(得分:0)

我认为这只是一个json对象,你可以创建一个具有相同属性(state,district等)的类并使用json serializer

或者您可以使用JObject

创建Json.Net

答案 2 :(得分:0)

您可以使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="checkdivwrp"> <input class='checkbox__toggle' type='checkbox'> <div class="serveTitle">title 1</div> </div> <div class="checkdivwrp"> <input class='checkbox__toggle' type='checkbox'> <div class="serveTitle">title 2</div> </div> <div class="checkdivwrp"> <input class='checkbox__toggle' type='checkbox'> <div class="serveTitle">title 3</div> </div>来进行序列化/反序列化作业,代码就是这样。

Newtonsonft.Json

然后,在设置对象后,只需序列化它。

public class Rootobject
    {
        [JsonProperty("State")]
        public string State { get; set; }
        [JsonProperty("District")]
        public string District { get; set; }
        [JsonProperty("Fact")]
        public Fact Fact { get; set; }
        [JsonProperty("Description")]
        public string Description { get; set; }
        [JsonProperty("CaseDate")]
        public string CaseDate { get; set; }
        [JsonProperty("FactNumber")]
        public string FactNumber { get; set; }
        [JsonProperty("FactType")]
        public string FactType { get; set; }
    }

    public class Fact
    {
        [JsonProperty("Id")]
        public string Id { get; set; }
    }

之后,您可以随时随地发送json。

答案 3 :(得分:0)

只需将{"Fact", "{Id,1}" }更改为{"Fact.Id", "1" },

即可