我想分析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个数据集来说非常顺利,但是之后,由于“一致性问题”,我难以对数据进行反序列化。文本的数据类型有时会更改为数组。
{
"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"
]
}
当数据显示出这种不一致时,我不知道如何反序列化数据。
答案 0 :(得分:6)
由于属性很复杂,您需要编写自己的反序列化逻辑。
这是我的,但这只是一个例子:
在这种情况下,我将获得“始终列表”结果,具有单个值的情况将只是具有一个条目的列表。
public List<TextProperty> text;
同样,如果是字符串,我会去找没有类型的“总是对象”
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();
}
结果如下: