【问题标题】:C# JSON String to Web APIC# JSON 字符串到 Web API
【发布时间】:2016-04-26 04:29:48
【问题描述】:

目前在 C# 中创建 JSON 字符串并尝试使用 WebClient 将其发送到 Web API 时遇到问题。

目前我有几种方法,首先是可行的方法。使用 POSTMAN 将以下文本数据集发送到 API 可以正常工作:

POSTMAN 数据集 - 这适用于 POSTMAN

{ "record": 
    {
        "form_id": "efe4f66f-b57c-4497-a370-25c0f3d8746a",
        "status": "240",
        "latitude": -82.638039,
        "longitude": 27.770787,
        "form_values": 
        {
            "833b": "99999",
            "683b": "9999999",
            "fa37": "Testing",
            "b2e3": "Testing"
        }
    }
}

在 C# 中,我使用了几种不同的方法来构建这种刺痛,但收效甚微。

C# 列表方法 - Newtonsoft.Json.Serialization

首先是构建一个List,然后使用Newtonsoft.Json来玩它:

    List<Parent> DataList = new List<Parent>();
    List<Child> Form = new List<Child.formvalues>();
    var Formvals = new Child.formvalues
    {
        ActionDescription = Row.ActionDescription,
        ActionNotes = Row.ActionNotes,
        ActionID = Row.ActionID,
        AssetID = Row.AssetID
    };
    Form.Add(Formvals);

    var DataElement = new Parent
    {
        form_id = AppFormID,
        latitude = Lat,
        longitude = Long,
        status = Row.Status,
        form_values = Form
    };
    DataList.Add(DataElement);

    string json = JsonConvert.SerializeObject(DataList.ToArray());

此代码产生以下字符串:

[
    {\"form_id\":\"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
    \"latitude\":-82.638039,
    \"longitude\":27.770787,
    \"status\":239,
    \"form_values\":[
        {\"833b\":99999,
        \"683b\":9999999,
        \"fa37\":\"Testing\",
        \"b2e3\":\"Testing\"
        }]
    }
]

我正在尝试的另一个尝试是以下,我认为这是迄今为止最接近的:

C# - 字符串构建方法

string Content = @"{ "
       + "\"record\": {"
       + "\"form_id\": "\"" + AppFormID + ""\","
       + "\"status\": "\"" + Row.Status + ""\","
       + "\"latitude\": "+ Lat  + ","
       + "\"longitude\": "+ Long + ","
       + "\"form_values\": {"
            + "\"833b\": "\""+Row.AssetID +""\","
            + "\"683b\": "\""+Row.ActionID + ""\","
            + "\"fa37\": "\""+Row.ActionDescription + ""\","
            + "\"b2e3\": "\""+Row.ActionNotes + ""\""
        + "}"
       + "}"
      + "}";

该行导致:

{ \"record\": 
    {
        \"form_id\": \"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
        \"status\": \"239\",
        \"latitude\": -82.638039,
        \"longitude\": 27.770787,
        \"form_values\": 
        {
            \"833b\": \"99999\",
            \"683b\": \"9999999\",
            \"fa37\": \"Testing\",
            \"b2e3\": \"Testing\"
        }
  }
}

问题!

那么问题来了,有人能帮我实现我放入 POSTMAN 的第一个 JSON 格式吗?

更新 - 下午 6:40

Web 客户端代码 c# AppURLRef 在代码中进一步设置,并且在调试器中似乎是正确的。 json 是测试的结果。

var http = new WebClient();
http.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var response = http.UploadString(AppURLRef, "POST", json);

更新结果

"[{\"record\":{\"form_id\":\"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
\"latitude\":-82.638039,
\"longitude\":27.770787,
\"status\":\"240\",
\"form_values\":         
[{\"833b\":\"99999\",
\"683b\":\"9999999\",
\"fa37\":\"Testing\",
\"b2e3\":\"Testing\"}]}}]"

【问题讨论】:

    标签: c# json


    【解决方案1】:

    试试下面的。

    public class Record
    {
      [JsonProperty(PropertyName = "form_id")]
      public string FormId { get; set; }
    
      [JsonProperty(PropertyName = "status")]
      public string Status { get; set; }
    
      [JsonProperty(PropertyName = "latitude")]
      public decimal Latitude { get; set; }
    
      [JsonProperty(PropertyName = "longitude")]
      public decimal Longitude { get; set; }
    
      [JsonProperty(PropertyName = "form_values")]
      public Dictionary<string, string> FormValues { get; set; }
    }
    
    public class RecordContainer
    {
      [JsonProperty(PropertyName = "record")]
      public Record Record { get; set; }
    }
    

    用法:

    var container = new RecordContainer();
    container.Record = new Record();
    // Code to populate values
    JsonConvert.SerializeObject(container);
    

    我正在使用 Newtonsoft Json 进行序列化,得到的输出与您要求的相同。

    【讨论】:

    • 试过这个模型,API 仍然报错数据 - 在调试器中停止它显示与 C# 相同的结果 - 字符串构建方法
    • 嗨@Caz1224,您如何使用Web 客户端将数据发送到Web Api?如果您有代码 sn-p,我可以尝试复制该问题。
    • 没有问题 - 我将使用 webclient 代码 sn-p 更新主要问题
    • 您缺少内容类型标头。请检查stackoverflow.com/questions/15091300/…中的第二个答案
    • 唷,很高兴它不是那么简单。解决这个问题,我仍然有我的问题。按照你的指示给我留下一个字符串: - 见更新 -
    【解决方案2】:

    我会与 NewtonSoft.JSON 一起尝试以下操作。

    var data = new
    {
        record = new
        {
            form_id = "efe4f66f-b57c-4497-a370-25c0f3d8746a",
            status = "240",
            latitude = -82.638039,
            longitude = 27.770787,
            form_values = new Dictionary<string, string>();
        }
    }
    
    data.record.form_values["833b"] = "99999";
    data.record.form_values["683b"] = "9999999";
    data.record.form_values["fa37"] = "Testing";
    data.record.form_values["b2e3"] = "Testing";
    

    然后获取 JSON:

    string json = JsonConvert.SerializeObject(data);
    

    【讨论】:

    • 唯一的问题是 833b 和 683b 不能是变量名,因为它们以数字开头。我假设你的意思是 JsonConvert.SerializeObject(data);在使用中?
    • 删除那些我仍然从服务器得到回扣说它不喜欢它
    • 我不得不废弃 WebClient() 它只是不起作用。我把 RestClient 放进去,然后用你的方法形成 JSON,它工作得很好。感谢您的帮助
    【解决方案3】:

    我认为它与this One 非常相似。您可以尝试以下代码来实现要求:

    var dd = {
    "FirstName": "ABC",
    "username": "abc123",
    "password": "abc@123",
    "Cnumbers": [{
        "Home": "0987654321"
    }, {
        "Company": "7654321"
    }]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      相关资源
      最近更新 更多