0

我在反序列化此 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 等。

有人能帮我吗?

4

3 回答 3

0

这个 json 是不可解析的,因为它不能被转换成任何具有这种“动态”键而不是键值对的类型。您应该使用的是JObject

快速示例:

JObject j = JObject.Parse(json);

var lst = j["posts"][0].Select(jp => ((JProperty)jp).Name).ToList();
于 2013-02-04T11:11:42.083 回答
0

您的项目中需要以下类:

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 );
    }
}
于 2013-02-04T16:48:07.623 回答
0

感谢你们俩; 我这样解决了:

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
                };
            }
        }
于 2013-02-06T16:06:05.233 回答