[英]Unable to de-serialize a JSON string with a dynamically changing node in C#
My JSON looks like this - 我的JSON看起来像这样-
{"0abc34m": {"time": "13 Mar 17, 4:50:02 PM", "pd": "oscar"}}
I am using this code to de-serialize this - 我正在使用此代码反序列化-
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(JSONstring));
ms.Position = 0;
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Rootobject));
Rootobject myDataTypObj = (Rootobject)jsonSerializer.ReadObject(ms);
I used Visual Studio function "Paste JSON as classes" function to generate these classes - 我使用了Visual Studio函数“将JSON作为类粘贴”函数来生成这些类-
public class Rootobject
{
public _00A462 _00a462 { get; set; }
}
public class _00A462
{
public string time { get; set; }
public string pd { get; set; }
}
I want to access the "time" and "pd" members of the JSON. 我想访问JSON的“时间”和“ pd”成员。
The first part of the JSON is a number that changes every time a new JSON string is received. JSON的第一部分是一个数字,每次接收到新的JSON字符串时都会更改。
I get no errors but my myDataTypObj has null values for _00A462. 我没有收到任何错误,但myDataTypObj的_00A462为空值。
I only care about the fields at the second level in the hierarchy.Am I approaching this correctly, what's the problem here? 我只关心层次结构中第二级的字段,我正确地解决了这个问题吗?
An easy way is to use JSON.Net and deserialize the JSON to a Dictionary
一种简单的方法是使用JSON.Net并将JSON反序列化为Dictionary
using Newtonsoft.Json;
class Program
{
static void Main( string[] args )
{
var jsonString = "{\"0abc34m\":{\"time\":\"13 Mar 17, 4:50:02 PM\",\"pd\":\"oscar\"}}";
Dictionary<string, Data> data;
data = JsonConvert.DeserializeObject<Dictionary<string, Data>>( jsonString );
var key = data.First().Key; // 0abc34m
var time = data.First().Value.TimeString; // 13 Mar 17, 4:50:02 PM
var pd = data.First().Value.DataString; // oscar
}
}
public class Data
{
[JsonProperty("time")]
public string TimeString { get; set; }
[JsonProperty("pd")]
public string DataString { get; set; }
}
Similar to Sir Rufo's Answer using Json.Net to dserialize to a JObject: 类似于Rufo爵士使用Json.Net来将其序列化为JObject的答案:
DotNetFiddle Example DotNetFiddle示例
var json = "{\"0abc34m\": {\"time\": \"13 Mar 17, 4:50:02 PM\", \"pd\": \"oscar\"}}";
dynamic d = JObject.Parse(json);
var values = (d.Properties() as IEnumerable<JProperty>)
.First()
.Value as JObject;
var time = DateTime.Parse(((values.Properties() as IEnumerable<JProperty>)
.First()
.Value as JValue).Value as string);
var pd = ((values.Properties() as IEnumerable<JProperty>)
.Skip(1)
.First()
.Value as JValue).Value as string;
Console.WriteLine(time);
Console.WriteLine(pd);
Result: 结果:
3/13/2017 4:50:02 PM
oscar
If you know JSON exact structure, you can read first (random) number, replace it in your string with known identifier and parse as JSON. 如果您知道JSON的确切结构,则可以读取第一个(随机)数字,将其替换为具有已知标识符的字符串,然后解析为JSON。
Next code needs debugging!!! 接下来的代码需要调试!!!
string json_string = ...;
int first_quot_pos = json_string.IndexOf("\"");
int second_quot_pos = json_string.IndexOf("\"", first_quot_pos + 1);
string random_number = json_string.Substring(first_quot_pos, second_quot_pos - first_quot_pos);
string updated_json_string = json_string.Replace(random_number, "number");
Your JSON now should look like this: 您的JSON现在应如下所示:
{"number": {"time": "13 Mar 17, 4:50:02 PM", "pd": "oscar"}}
And then use updated_json_string
: 然后使用updated_json_string
:
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(updated_json_string));
ms.Position = 0;
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Rootobject));
Rootobject myDataTypObj = (Rootobject)jsonSerializer.ReadObject(ms);
Your Rootobject
should now look like this: 现在,您的Rootobject
应该看起来像这样:
public class Rootobject
{
public Number number { get; set; }
}
public class Number
{
public string time { get; set; }
public string pd { get; set; }
}
Now you have object which have myDataTypObj.number.time
and myDataTypObj.number.pd
. 现在,您有了具有myDataTypObj.number.time
和myDataTypObj.number.pd
对象。 Also you have random_number
. 你也有random_number
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.