How can I deserialize JSON containing delimited JSON?(如何反序列化包含分隔 JSON 的 JSON?)
问题描述
我在将 Json 字符串反序列化为对象时遇到问题.
I have a problem with deserializing a Json-string to an object.
这是我从网络服务收到的示例 json:
This is a sample json i receive from a webservice:
{
"GetDataResult":
"{
"id":1234,
"cityname":"New York",
"temperature":300,
}"
}
我有一个像这样的类 CityData
And I have a class CityData that looks like this
[JsonObject("GetDataResult")]
public class CityData
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("cityname")]
public string CityName { get; set; }
[JsonProperty("temperature")]
public int Temperature { get; set; }
}
我尝试通过调用 DeserializeObject 方法来反序列化 json
I try to deserialize the json with a call of the method DeserializeObject
var cityData = JsonConvert.DeserializeObject<CityData>(response);
但根元素似乎有问题......
but the root element seems to make problems...
你们知道我该如何解决它,以便我收到一个填写了数据的 CityData 对象吗?
Do you guys know how I can fix it, so that I receive a CityData-object with the data filled in?
推荐答案
json 响应包含一个对象,该对象本身包含一个表示数据结果的 json 字符串.
The json response contains an object that within itself contains a json string representing the data result.
您需要反序列化两次,一次用于响应,一次用于数据结果.
You need to deserialize twice, once for the response and one more for the data result.
var response = JsonConvert.DeserializeObject<JObject>(responseStr);
var dataResult = (string)response["GetDataResult"];
var cityData = JsonConvert.DeserializeObject<CityData>(dataResult);
这篇关于如何反序列化包含分隔 JSON 的 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何反序列化包含分隔 JSON 的 JSON?


基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01