Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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/8/sorting/2.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# “ReadAsAsync<;字符串>;`和'ReadAsStringAsync'是否用于?_C#_Asp.net_Json.net - Fatal编程技术网

C# “ReadAsAsync<;字符串>;`和'ReadAsStringAsync'是否用于?

C# “ReadAsAsync<;字符串>;`和'ReadAsStringAsync'是否用于?,c#,asp.net,json.net,C#,Asp.net,Json.net,HttpContentExtensions.ReadAsAsync和HttpContent.ReadAsStringAsync应该用于什么 他们似乎在做相似的事情,但工作方式却很奇怪。下面是一些测试及其输出。在某些情况下会抛出JsonReaderException,在某些情况下,会输出JSON,但会附加转义字符 我最终在我的代码库中使用了这两个函数,但如果我能理解它们的工作原理,我希望在其中一个函数上保持一致 //Create data and serialise to JSON var dat

HttpContentExtensions.ReadAsAsync
HttpContent.ReadAsStringAsync
应该用于什么

他们似乎在做相似的事情,但工作方式却很奇怪。下面是一些测试及其输出。在某些情况下会抛出
JsonReaderException
,在某些情况下,会输出JSON,但会附加转义字符

我最终在我的代码库中使用了这两个函数,但如果我能理解它们的工作原理,我希望在其中一个函数上保持一致

//Create data and serialise to JSON
var data = new
{
    message = "hello world"
};
string dataAsJson = JsonConvert.SerializeObject(data);

//Create request with data
HttpConfiguration config = new HttpConfiguration();
HttpRequestMessage request = new HttpRequestMessage();
request.SetConfiguration(config);
request.Method = HttpMethod.Post;
request.Content = new StringContent(dataAsJson, Encoding.UTF8, "application/json");

string requestContentT = request.Content.ReadAsAsync<string>().Result; // -> JsonReaderException: Error reading string.Unexpected token: StartObject.Path '', line 1, position 1.
string requestContentS = request.Content.ReadAsStringAsync().Result; // -> "{\"message\":\"hello world\"}"

//Create response from request with same data
HttpResponseMessage responseFromRequest = request.CreateResponse(HttpStatusCode.OK, dataAsJson, "application/json");

string responseFromRequestContentT = responseFromRequest.Content.ReadAsAsync<string>().Result; // -> "{\"message\":\"hello world\"}"
string responseFromRequestContentS = responseFromRequest.Content.ReadAsStringAsync().Result; // -> "\"{\\\"message\\\":\\\"hello world\\\"}\""

//Create a standalone new response
HttpResponseMessage responseNew = new HttpResponseMessage();
responseNew.Content = new StringContent(dataAsJson, Encoding.UTF8, "application/json");

string responseNewContentT = responseNew.Content.ReadAsAsync<string>().Result; // -> JsonReaderException: Error reading string.Unexpected token: StartObject.Path '', line 1, position 1.
string responseNewContentS = responseNew.Content.ReadAsStringAsync().Result; // -> "{\"message\":\"hello world\"}"
//创建数据并序列化为JSON
var数据=新
{
message=“你好,世界”
};
字符串dataAsJson=JsonConvert.SerializeObject(数据);
//使用数据创建请求
HttpConfiguration config=新的HttpConfiguration();
HttpRequestMessage请求=新建HttpRequestMessage();
SetConfiguration(config);
request.Method=HttpMethod.Post;
request.Content=newstringcontent(dataAsJson,Encoding.UTF8,“application/json”);
string requestContentT=request.Content.ReadAsAsync().Result;//->JsonReaderException:读取字符串时出错。意外标记:StartObject.Path“”,第1行,位置1。
string requestContentS=request.Content.ReadAsStringAsync().Result;//->“{\'message\':\'hello world\'}”
//从具有相同数据的请求创建响应
HttpResponseMessageResponseFromRequest=request.CreateResponse(HttpStatusCode.OK,dataAsJson,“应用程序/json”);
字符串responseFromRequestContentT=responseFromRequest.Content.ReadAsAsAsync().Result;//->“{\'message\':\'hello world\'}”
字符串responseFromRequestContentS=responseFromRequest.Content.ReadAsStringAsync().Result;//->“\”{\\\”消息\\\\:\\\\“你好,世界\\\\”}”
//创建独立的新响应
HttpResponseMessage responseNew=新的HttpResponseMessage();
responseNew.Content=newstringcontent(dataAsJson,Encoding.UTF8,“application/json”);
字符串responseNewContentT=responseNew.Content.ReadAsAsync().Result;//->JsonReaderException:读取字符串时出错。意外标记:StartObject.Path“”,第1行,位置1。
字符串responseNewContentS=responseNew.Content.ReadAsStringAsync().Result;//->“{\'message\':\'hello world\'}”
:这是一个基本的“以字符串形式获取内容”方法。它会对你扔给它的任何东西起作用,因为它只是一根弦

:这用于将JSON响应反序列化到对象中。它失败的原因是,返回中的JSON不是单个字符串的有效JSON表示形式。例如,如果序列化字符串:

var result = JsonConvert.SerializeObject("hello world");
Console.WriteLine(result);
输出为:

"hello world"

注意它是如何被双引号包围的。如果您试图将任意JSON直接反序列化为一个非
“…”
格式的字符串,它将抛出您看到的异常,因为它希望JSON以
开头,
ReadAsAsync

readasync
可能只是
jsoninvert.DeserializeObject(wait readastringasync())的包装器
@CamiloTerevinto实际上不是。
ReadAsAsync
比它聪明得多,因为它不会先把字符串拉到内存中,然后创建对象。@CamiloTerevinto有趣的是,大卫·福勒(如果你不知道他是谁……你真是太遗憾了,哈哈)在推特上就有一条很棒的帖子@CamiloTerevinto我不是一个推特爱好者,但是Fowler和Damian Edwards很好地关注,并且对回答问题很有反应。哦,ASP.NET社区直播站()也非常有用。