我在反序列化此JSON响应时遇到问题
{
"posts": {
"Pippo": {
"text": "text1",
"link": "link1"
},
"Pluto": {
"text": "text2",
"link": "link2"
}
}
}
我用的是这个型号的
public class postModel
{
public string text { get; set; }
public string link { get; set; }
}
public class postFields
{
public postModel post { get; set; }
}
public class RootObject
{
public Dictionary<string, postFields> posts { get; set; }
}
然后我用这种方式反序列化
var deserialized = JsonConvert.DeserializeObject<RootObject>(json);
然后我停了下来。我无法读取值,因为我尝试了此
foreach (var value in deserialized)
{
new postModel
{
text = value.Value.post.text,
link = value.Value.post.link
};
}
然后我得到了NullReferenceException,因为JSON属性的名称不是"post",而是Pippo、Pluto等。
有人能帮我吗?
感谢你们两位;我是这样解决的:
var main = JObject.Parse(json);
foreach (var mainRoute in main.Properties()) // this is "posts"
{
foreach (var subRoute in mainRoute.Values<JObject>().SelectMany(x => x.Properties())) // this is "Pippo", "Pluto"
{
var deserialized = JsonConvert.DeserializeObject<postModel>(subRoute.Value.ToString());
new postModel
{
text = deserialized.text,
link = deserialized.link
};
}
}
这个json是不可解析的,因为它不能用这样的"动态"键而不是键值对转换成任何类型。您应该使用JObject。
快速取样:
JObject j = JObject.Parse(json);
var lst = j["posts"][0].Select(jp => ((JProperty)jp).Name).ToList();
您的项目中需要以下类:
namespace Newtonsoft.Json
{
public class DictionaryConverter<tKey, tValue>: JsonConverter
{
public override bool CanRead { get { return true; } }
public override bool CanWrite { get { return true; } }
public override bool CanConvert( Type objectType )
{
if( !objectType.IsGenericType )
return false;
if( objectType.GetGenericTypeDefinition() != typeof( Dictionary<,> ) )
return false;
Type[] argTypes = objectType.GetGenericArguments();
if( argTypes.Length != 2 || argTypes[ 0 ] != typeof( tKey ) || argTypes[ 1 ] != typeof( tValue ) )
return false;
return true;
}
public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
{
do
{
if( JsonToken.StartObject != reader.TokenType )
break;
Dictionary<tKey, tValue> res = new Dictionary<tKey, tValue>();
while( reader.Read() )
{
if( JsonToken.EndObject == reader.TokenType )
return res;
if( JsonToken.PropertyName == reader.TokenType )
{
tKey key = (tKey)Convert.ChangeType( reader.Value, typeof( tKey ), null );
if( !reader.Read() )
break;
tValue val = serializer.Deserialize<tValue>( reader );
res[ key ] = val;
}
}
}
while( false );
throw new Exception( "unexpected JSON" );
}
public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
{
if( null == value )
return;
Dictionary<tKey, tValue> src = value as Dictionary<tKey, tValue>;
if( null == src )
throw new Exception( "Expected Dictionary<{0}, {1}>".FormatWith( typeof( tKey ).Name, typeof( tValue ).Name ) );
writer.WriteStartObject();
foreach (var kvp in src)
{
string strKey = (string)Convert.ChangeType( kvp.Key, typeof( string ), null );
writer.WritePropertyName( strKey );
serializer.Serialize( writer, kvp.Value );
}
writer.WriteEndObject();
}
}
}
然后,您可以通过以下方式修改您的RootObject:
public class RootObject
{
[ JsonProperty, JsonConverter( typeof( DictionaryConverter<string, postModel> ) ) ]
public Dictionary<string, postModel> posts;
}
然后一切都会如你所愿。
更新:这是我忘记的实用方法:
public static class SharedUtils
{
public static string FormatWith( this string format, params object[] args )
{
if( format == null )
throw new ArgumentNullException( "format" );
return String.Format( format, args );
}
}