反序列化JSON-一个键有多种数据类型的文件
Deserialize JSON-File with multiple datatypes for a key
我想分析 Telegram-Chats,所以我以 JSON 格式导出聊天,并想将其反序列化到我的分析软件中。
{
"id": 397910,
"type": "message",
"date": "2018-02-21T10:27:59",
"edited": "1970-01-01T01:00:00",
"from": "Username",
"from_id": 39033284,
"text": "Some Text"
}
所以我使用这个简单的代码来阅读 JSON
List<JSONObject> jsonObjects = JsonConvert.DeserializeObject<List<JSONObject>>(File.ReadAllText(openFileDialog.FileName));
public class JSONObject
{
public int ID;
public string type;
public string date;
public string edited;
public string from;
public int fromID;
public string photo;
public int width;
public int height;
public string text;
}
这对于前 525 个数据集来说非常顺利,但之后,由于 "consistency issues",我在反序列化数据时遇到了麻烦。文本的数据类型有时会更改为数组。
{
"id": 397911,
"type": "message",
"date": "2018-02-21T10:31:47",
"edited": "1970-01-01T01:00:00",
"from": "Username",
"from_id": 272964614,
"text": [
"Some Text ",
{
"type": "mention",
"text": "@school"
},
" Some Text"
]
}
另外,我找到了这个数据集
{
"id": 397904,
"type": "message",
"date": "2018-02-21T10:18:12",
"edited": "1970-01-01T01:00:00",
"from": "Username",
"from_id": 39033284,
"text": [
{
"type": "link",
"text": "google.com"
},
"\n\nSome Text"
]
}
当数据显示这种不一致时,我不知道如何反序列化数据。
由于您的 属性 很复杂,您需要编写自己的 de-serialization 逻辑。
这是我的,但这只是一个例子:
- 首先,您的文字 属性 似乎是
- 单个值
- 或值数组
在这种情况下,我会选择 "always list" 结果,具有单个值的情况将只是一个包含一个条目的列表。
public List<TextProperty> text;
- 数值还可以
- 单个字符串值
- 具有字符串值和元数据(文本类型)的对象
同样,如果它只是字符串,我将选择 "always object" 没有类型
public class TextProperty
{
public string text { get; set; }
public string type { get; set; }
}
然后你必须制作自己的 Converter 来处理这个,你只需要继承 JsonConverter 并实现逻辑
public class TextPropertyConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException(); // not covered here
}
// A value can be either single string or object
// Return a TextProperty in both cases
private TextProperty ParseValue(JToken value)
{
switch(value.Type)
{
case JTokenType.String:
return new TextProperty { text = value.ToObject<string>() };
case JTokenType.Object:
return value.ToObject<TextProperty>();
default:
return null;
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// You'll start either with a single value (we'll convert to list of one value) or an array (list of several values then)
switch(reader.TokenType)
{
case JsonToken.String:
case JsonToken.StartObject:
return new List<TextProperty> { ParseValue(JToken.Load(reader)) };
case JsonToken.StartArray:
var a = JArray.Load(reader);
var l = new List<TextProperty>();
foreach(var v in a)
l.Add(ParseValue(v));
return l;
default:
return null;
}
}
public override bool CanConvert(Type objectType) => false;
}
我认为应该涵盖所有情况
要使用它,只需将 JsonConverter 属性添加到目标 属性
public class JSONObject
{
public int id;
public string type;
public string date;
public string edited;
public string from;
public int from_id;
public string photo;
public int width;
public int height;
[JsonConverter(typeof(TextPropertyConverter))]
public List<TextProperty> text;
}
然后测试一下:
static void Main(string[] args)
{
string json = @"
[
{
""id"": 397910,
""type"": ""message"",
""date"": ""2018-02-21T10:27:59"",
""edited"": ""1970-01-01T01:00:00"",
""from"": ""Username"",
""from_id"": 39033284,
""text"": ""Some Text""
},
{
""id"": 397911,
""type"": ""message"",
""date"": ""2018-02-21T10:31:47"",
""edited"": ""1970-01-01T01:00:00"",
""from"": ""Username"",
""from_id"": 272964614,
""text"": [
""Some Text "",
{
""type"": ""mention"",
""text"": ""@school""
},
"" Some Text""
]
}
]";
List<JSONObject> jsonObjects = JsonConvert.DeserializeObject<List<JSONObject>>(json);
Console.Read();
}
结果如下:
我想分析 Telegram-Chats,所以我以 JSON 格式导出聊天,并想将其反序列化到我的分析软件中。
{
"id": 397910,
"type": "message",
"date": "2018-02-21T10:27:59",
"edited": "1970-01-01T01:00:00",
"from": "Username",
"from_id": 39033284,
"text": "Some Text"
}
所以我使用这个简单的代码来阅读 JSON
List<JSONObject> jsonObjects = JsonConvert.DeserializeObject<List<JSONObject>>(File.ReadAllText(openFileDialog.FileName));
public class JSONObject
{
public int ID;
public string type;
public string date;
public string edited;
public string from;
public int fromID;
public string photo;
public int width;
public int height;
public string text;
}
这对于前 525 个数据集来说非常顺利,但之后,由于 "consistency issues",我在反序列化数据时遇到了麻烦。文本的数据类型有时会更改为数组。
{
"id": 397911,
"type": "message",
"date": "2018-02-21T10:31:47",
"edited": "1970-01-01T01:00:00",
"from": "Username",
"from_id": 272964614,
"text": [
"Some Text ",
{
"type": "mention",
"text": "@school"
},
" Some Text"
]
}
另外,我找到了这个数据集
{
"id": 397904,
"type": "message",
"date": "2018-02-21T10:18:12",
"edited": "1970-01-01T01:00:00",
"from": "Username",
"from_id": 39033284,
"text": [
{
"type": "link",
"text": "google.com"
},
"\n\nSome Text"
]
}
当数据显示这种不一致时,我不知道如何反序列化数据。
由于您的 属性 很复杂,您需要编写自己的 de-serialization 逻辑。
这是我的,但这只是一个例子:
- 首先,您的文字 属性 似乎是
- 单个值
- 或值数组
在这种情况下,我会选择 "always list" 结果,具有单个值的情况将只是一个包含一个条目的列表。
public List<TextProperty> text;
- 数值还可以
- 单个字符串值
- 具有字符串值和元数据(文本类型)的对象
同样,如果它只是字符串,我将选择 "always object" 没有类型
public class TextProperty
{
public string text { get; set; }
public string type { get; set; }
}
然后你必须制作自己的 Converter 来处理这个,你只需要继承 JsonConverter 并实现逻辑
public class TextPropertyConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException(); // not covered here
}
// A value can be either single string or object
// Return a TextProperty in both cases
private TextProperty ParseValue(JToken value)
{
switch(value.Type)
{
case JTokenType.String:
return new TextProperty { text = value.ToObject<string>() };
case JTokenType.Object:
return value.ToObject<TextProperty>();
default:
return null;
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// You'll start either with a single value (we'll convert to list of one value) or an array (list of several values then)
switch(reader.TokenType)
{
case JsonToken.String:
case JsonToken.StartObject:
return new List<TextProperty> { ParseValue(JToken.Load(reader)) };
case JsonToken.StartArray:
var a = JArray.Load(reader);
var l = new List<TextProperty>();
foreach(var v in a)
l.Add(ParseValue(v));
return l;
default:
return null;
}
}
public override bool CanConvert(Type objectType) => false;
}
我认为应该涵盖所有情况
要使用它,只需将 JsonConverter 属性添加到目标 属性
public class JSONObject
{
public int id;
public string type;
public string date;
public string edited;
public string from;
public int from_id;
public string photo;
public int width;
public int height;
[JsonConverter(typeof(TextPropertyConverter))]
public List<TextProperty> text;
}
然后测试一下:
static void Main(string[] args)
{
string json = @"
[
{
""id"": 397910,
""type"": ""message"",
""date"": ""2018-02-21T10:27:59"",
""edited"": ""1970-01-01T01:00:00"",
""from"": ""Username"",
""from_id"": 39033284,
""text"": ""Some Text""
},
{
""id"": 397911,
""type"": ""message"",
""date"": ""2018-02-21T10:31:47"",
""edited"": ""1970-01-01T01:00:00"",
""from"": ""Username"",
""from_id"": 272964614,
""text"": [
""Some Text "",
{
""type"": ""mention"",
""text"": ""@school""
},
"" Some Text""
]
}
]";
List<JSONObject> jsonObjects = JsonConvert.DeserializeObject<List<JSONObject>>(json);
Console.Read();
}
结果如下: