将json字符串反序列化为C#中的对象变量

时间:2018-05-23 23:28:00

标签: c# json type-conversion deserialization

我有以下代码:

var response = await client.PostAsync("http://localhost/test-request", content);

var responseString = await response.Content.ReadAsStringAsync();
var responseJSON = JsonConvert.DeserializeObject(responseString);

Console.WriteLine("Finished");

MessageBox.Show("Hi", responseJSON.value, MessageBoxButtons.OK);

responseString正确返回,但我正在尝试将其转换为对象,因此我可以使用返回值执行更多高级内容。

问题是,C#/ Visual Studio抱怨responseJSON.value没有值(在Async操作完成之前它没有)

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

var responseJSON = JsonConvert.DeserializeObject(responseString);

将responseJSON创建为JSON.net JSObject。

你需要

var responseJSON = JsonConvert.DeserializeObject<SomeObject>(responseString);

dynamic responseJSON = JsonConvert.DeserializeObject(responseString);