微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

内部列表不会使用 httpclient c# 在服务器上填充

如何解决内部列表不会使用 httpclient c# 在服务器上填充

我遇到了一个问题,我试图发布一个具有多个属性的类,但有些属性属于 List 类型,但由于某种原因没有在服务器上发布。

这是来自网络服务器的项目的示例 JSON

{
 "at": "AA","campaign": "UnkNown","createdby": {
  "userid": "user001","writetime": "2021-06-13T23:43:21.600446Z"
 },"geographies": [
  {
   "country": "USA","province": "New York","city": "Albany","createdby": {
    "userid": "user001","writetime": "2020-06-17T15:46:45.214185Z"
   },"id": "db3faf37-88d3-46fd-8ca5-54898c3450fc","source": "Manual Entry","userid": ""user001",","writetime": "2020-06-17T15:46:45.214185Z"
  }
 ],"isverified": true,"isValid": false,"platforms": [],"remarks": {},"roe": "001","dtg": "2021-06-13T16:42:00Z","identifier": "10101","type": "DELIBERATE","id": "cd4e2c93-4215-466c-b680-044a094477b5","rowid": "e70e9253-1946-4497-ba8e-58216c1a1e39","userid": "user001","writetime": "2021-06-13T23:43:21.600446Z","readgroups": [
  "public"
 ],"writegroups": [
  "public"
 ],"classification": {
  "level": "0","aea": {
   "value": ""
  },"classificationString": "","portionString": ""
 }
}

当我使用 C# 类创建我自己的项目时,我将它序列化,这就是它的样子

{
  "at": "FE","writetime": "2021-06-27T12:00:00"
  },"geographies": [
    {
      "country": "USA","province": "Florida","city": "Miami","createdby": {
        "userid": "user001","writetime": "2021-06-27T12:00:00"
      },"writetime": "0001-01-01T00:00:00"
    }
  ],"munitions": [],"operation": null,"remarks": {
    "remark1": null,"remark2": null
  },"roe": "009","dtg": "2021-06-27T12:00:00","identifier": "77896","type": "DYNAMIC","unit": null,"source": null,"writetime": "0001-01-01T00:00:00","readgroups": [
    "public"
  ],"writegroups": [
    "public"
  ],"classification": null
}

并且帖子的结果是成功的,我在网络服务器上看到了它,但由于某种原因没有发布地理位置。

如果有帮助,这是我的 C# 代码

MyItemClass item;           

item = new MyItemClass
{
    id = _id,rowid = _rowid,at = _at,campaign = _campaign,isverified = _verified,isValid = _valid,operation = _operation,roe = _roe,dtg = _dtg,identifier = _identifier,type = _type,unit = _unit,userid = _userId,createdby = new CreatedByClass()
    {
        userid = _userId,writetime = _writetime
    },remarks = new RemarksClass()
    {
        remark1 = _rem1,remark2 = _rem2
    },readgroups = new List<string>() { "public" },writegroups = new List<string>() { "public" },geographies = new List<Geography>(),platforms = new List<Platform>(),};

// import Location data
foreach (LocationsModel location in listofLocations)
{
    if (location.SelectedCity == null)
        location.SelectedCity = string.Empty;
    if (location.SelectedProvince == null)
        location.SelectedProvince = string.Empty;

    item.geographies.Add(new Geography()
    {
        city = location.SelectedCity,country = location.SelectedCountry,province = location.SelectedProvince,id = location.LocationGuid.ToString(),userid = _userid,source = _source,createdby = new CreatedByClass()
    });
}

string ItemJsonString = JsonConvert.SerializeObject(item);
JObject data = JObject.Parse(ItemJsonString);

string newJson = data.ToString();

var Content = new StringContent(newJson,Encoding.UTF8,"application/json");
var result = httpClient?.PostAsync($"<URL>",Content).Result;
            

从网上的资料来看,我看到了如何执行 POST 的方法很少,这是其中之一,但根据我的尝试,它们都没有奏效,我不知道为什么。

解决方法

试试这个。它已在 Visual Studio 中测试并正常运行

数据

    var jsonString = "{\"at\":\"AA\",\"campaign\":\"Unknown\",\"createdby\":{\"userid\":\"user001\",\"writetime\":\"2021-06-13T23:43:21.600446Z\"},\"geographies\":[{\"country\":\"USA\",\"province\":\"New York\",\"city\":\"Albany\",\"writetime\":\"2020-06-17T15:46:45.214185Z\"},\"id\":\"db3faf37-88d3-46fd-8ca5-54898c3450fc\",\"source\":\"Manual Entry\",\"userid\":\"user001\",\"writetime\":\"2020-06-17T15:46:45.214185Z\"}],\"isVerified\":true,\"isValid\":false,\"platforms\":[],\"remarks\":{},\"roe\":\"001\",\"dtg\":\"2021-06-13T16:42:00Z\",\"identifier\":\"10101\",\"type\":\"DELIBERATE\",\"id\":\"cd4e2c93-4215-466c-b680-044a094477b5\",\"rowid\":\"e70e9253-1946-4497-ba8e-58216c1a1e39\",\"writetime\":\"2021-06-13T23:43:21.600446Z\",\"readgroups\":[\"public\"],\"writegroups\":[\"public\"],\"classification\":{\"level\":\"0\",\"aea\":{\"value\":\"\"},\"classificationString\":\"\",\"portionString\":\"\"}}";
    

代码

 var Content = new StringContent(jsonString,Encoding.UTF8,"application/json");
var result = httpClient?.PostAsync($"<URL>",Content).Result;

这个也测试过

代码


var jsonObj = JsonConvert.DeserializeObject<Data>(jsonString);
var json = JsonConvert.SerializeObject(jsonObj);
    
var content = new StringContent(json,"application/json");
var response = await client.PostAsync(uri,content);
    
    if (response.IsSuccessStatusCode)
    {
        var stringData = await response.Content.ReadAsStringAsync();
        var result = JsonConvert.DeserializeObject<Data>(stringData);
    }

课程


public class Createdby
{
    public string userid { get; set; }
    public DateTime writetime { get; set; }
}

public class Geography
{
    public string country { get; set; }
    public string province { get; set; }
    public string city { get; set; }
    public Createdby createdby { get; set; }
    public string id { get; set; }
    public string source { get; set; }
    public string userid { get; set; }
    public DateTime writetime { get; set; }
}

public class Remarks
{
    public string remark1 { get; set; }
    public string remark2 { get; set; }
}

public class Aea
{
    public string value { get; set; }
}

public class Classification
{
    public string level { get; set; }
    public Aea aea { get; set; }
    public string classificationString { get; set; }
    public string portionString { get; set; }
}

public class Data
{
    public string at { get; set; }
    public string campaign { get; set; }
    public Createdby createdby { get; set; }
    public List<Geography> geographies { get; set; }
    public bool isVerified { get; set; }
    public bool isValid { get; set; }
    public List<object> platforms { get; set; }
    public Remarks remarks { get; set; }
    public string roe { get; set; }
    public DateTime dtg { get; set; }
    public string identifier { get; set; }
    public string type { get; set; }
    public string id { get; set; }
    public string rowid { get; set; }
    public string source { get; set; }
    public string userid { get; set; }
    public DateTime writetime { get; set; }
    public List<string> readgroups { get; set; }
    public List<string> writegroups { get; set; }
    public Classification classification { get; set; }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。