Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用HttpClient发送JSON对象_C# - Fatal编程技术网

C# 使用HttpClient发送JSON对象

C# 使用HttpClient发送JSON对象,c#,C#,嗯,我知道可以使用HttpClient.sendsync()发送HttpRequestMessage,对于简单字符串,可以将请求方法设置为POST,并将内容设置为StringContent。。。但在我的例子中,我想发送一个更复杂的JSON字符串,如下所示 { "requests": [ { "image": { "content": "" }, "features": [ { "type": "U

嗯,我知道可以使用
HttpClient.sendsync()
发送
HttpRequestMessage
,对于简单字符串,可以将请求方法设置为POST,并将内容设置为
StringContent
。。。但在我的例子中,我想发送一个更复杂的
JSON
字符串,如下所示

{
  "requests": [
    {
      "image": {
        "content": ""
      },
      "features": [
        {
          "type": "UNSPECIFIED",
          "maxResults": 50
        }
      ]
    }
  ]
}
我尝试使用JavaScriptSerializer,但不知道如何创建一个表示json的对象

await Browser.SendAsync(new HttpRequestMessage
{
    RequestUri = new Uri("http://127.0.0.1/"),
    Method = HttpMethod.Post,
    Content = new StringContent(new JavaScriptSerializer().Serialize())
});

将类创建为@x。。。在评论中指出,以建立您的树

public class features
{
   public string type {get;set;}
   public int maxResults {get;set;}
}
public class requests 
{
   public List<features> {get;set;}
   ... the same for images
}
公共类功能
{
公共字符串类型{get;set;}
public int maxResults{get;set;}
}
公共类请求
{
公共列表{get;set;}
…图像也是如此
}

填充它,序列化它并发送请求…

将类创建为@x。。。在评论中指出,以建立您的树

public class features
{
   public string type {get;set;}
   public int maxResults {get;set;}
}
public class requests 
{
   public List<features> {get;set;}
   ... the same for images
}
公共类功能
{
公共字符串类型{get;set;}
public int maxResults{get;set;}
}
公共类请求
{
公共列表{get;set;}
…图像也是如此
}
填充它,序列化它并发送请求…

如果您想要此对象的C代码,请使用RootObject类

public class Image
{
    public string content { get; set; }
}

public class Feature
{
    public string type { get; set; }
    public int maxResults { get; set; }
}

public class Request
{
    public Image image { get; set; }
    public List<Feature> features { get; set; }
}

public class RootObject
{
    public List<Request> requests { get; set; }
}
公共类映像
{
公共字符串内容{get;set;}
}
公共类功能
{
公共字符串类型{get;set;}
public int maxResults{get;set;}
}
公共类请求
{
公共映像映像{get;set;}
公共列表功能{get;set;}
}
公共类根对象
{
公共列表请求{get;set;}
}

提供如果需要此对象的C#代码,请使用RootObject类

public class Image
{
    public string content { get; set; }
}

public class Feature
{
    public string type { get; set; }
    public int maxResults { get; set; }
}

public class Request
{
    public Image image { get; set; }
    public List<Feature> features { get; set; }
}

public class RootObject
{
    public List<Request> requests { get; set; }
}
公共类映像
{
公共字符串内容{get;set;}
}
公共类功能
{
公共字符串类型{get;set;}
public int maxResults{get;set;}
}
公共类请求
{
公共映像映像{get;set;}
公共列表功能{get;set;}
}
公共类根对象
{
公共列表请求{get;set;}
}

由于

已经是json,您无法进一步序列化它。只需将json用作
StringContent
@Crowcoder,我可以创建一个表示此树的类吗?如果是这样的话,它应该是什么样的呢?我不想序列化这个字符串,我想按需创建一个类似这样的字符串,创建一个类,它有这样的结构。填满它。将其序列化为字符串。然后通过HttpClient发送。如果您创建了一个类,您可以让HttpClient的扩展为您处理序列化:“@Crowcoder将该类序列化为这种格式应该是什么样子?”您可以用一个类提供答案吗?该类已经是json,您无法进一步序列化它。只需将json用作
StringContent
@Crowcoder,我可以创建一个表示此树的类吗?如果是这样的话,它应该是什么样的呢?我不想序列化这个字符串,我想按需创建一个类似这样的字符串,创建一个类,它有这样的结构。填满它。将其序列化为字符串。然后通过HttpClient发送。如果您创建了一个类,您可以让HttpClient的扩展为您处理序列化:“@Crowcoder将该类序列化为这种格式应该是什么样的?”您可以用一个类提供答案吗?我总是尝试走懒散的路;)我总是试着走懒散的路;)