2017-08-13 84 views
0

我通过POST检索以下JSON通过API如何创建一个词典<字符串,字符串>嵌套对象

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

我能够执行通过JavaScript Ajax请求,但我也希望通过C#代码来使用API​​。

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

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?

+0

我不熟悉这个领域,但我想你应该写' “{ID:1}”',而不是' “{ID,1}”'? – Sweeper

+0

你问的是不是不可能的,但我建议不要在这里使用'Dictionary '。一旦你的JSON对象不再只是一个平坦的属性列表,并开始嵌套属性等,你会更好的定义一个自定义的类,以匹配你期望的JSON对象。这也消除了通过索引(字符串)查找来查找属性的需要,并使得它们可以通过定义的属性直接访问。您目前的问题可以得到解答,但在您的情况下,这似乎是一个糟糕的方法。 – Flater

+0

为什么不使用Json格式? – Valkyrie

回答

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); 
0

我认为这仅仅是一个JSON对象,您可以创建具有的(国家,地区等)相同属性的类,并使用json serializer

,或者您可以使用创建JObjectJson.Net

0

参考您可以使用Newtonsonft.Json向serializaton /反序列化的工作,代码会这样。

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; } 
    } 

然后,在安装好对象之后,将其序列化。

Rootobject example = new Rootobject(); 
//Add values to the variable example. 
var objectSerialized = JsonConvert.SerializeObject(example); 

之后,你将有一个json准备发送到任何你想要的地方。

0

只要改变{"Fact", "{Id,1}" }{"Fact.Id", "1" },

相关问题