当键包含Dollar($)(Metadata)时将JSON反序列化为C#对象

时间:2018-12-04 05:24:08

标签: c# json json-deserialization

所以我得到这样的答复

{"$id":"1","success":true,"errors":{"$id":"2","$values":[]}}

我如何将其转换为c#对象,尝试使用this(http://json2csharp.com/)工具进行输出,但这没有任何意义 这就是我要得到的

x

public class Errors
{
    public string __invalid_name__$id { get; set; }
    public List<object> __invalid_name__$values { get; set; }
}

public class RootObject
{
    public string __invalid_name__$id { get; set; }
    public bool success { get; set; }
    public Errors errors { get; set; }
}

我是C#的新手,我将不胜感激任何输入,我基本上需要访问成功键变量

4 个答案:

答案 0 :(得分:3)

您需要将[JsonProperty]属性添加到键名以美元$开头的每个属性中

public class Errors
{
    [JsonProperty("$id")]
    public string id { get; set; }

    [JsonProperty("$values")]
    public List<object> values { get; set; }
}

public class RootObject
{
    [JsonProperty("$id")]
    public string id { get; set; }
    public bool success { get; set; }
    public Errors errors { get; set; }
}

因为$表示元数据,而不是实际的数据字段。因此您必须修改JsonSerializerSettings才能忽略MetadataPropertyHandling。

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

最后将您的json反序列化为上述类对象。

RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json, settings);

在这里,我创建了一个示例控制台应用程序用于演示,该应用程序演示了上面的代码将如何工作。

class program
{
    public static void Main()
    {
        string json = File.ReadAllText(@"Path to your json file");

        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

        RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json, settings);

        Console.WriteLine("id: " + rootObject.id);
        Console.WriteLine("success: " + rootObject.success);
        Console.WriteLine("errors.id: " + rootObject.errors.id);
        Console.WriteLine("errors.values: " + string.Join(",", rootObject.errors.values));
        Console.ReadLine();
    }
}

输出:

enter image description here

答案 1 :(得分:1)

好吧,你能做的就是

public class Errors
{
    [JsonProperty(PropertyName = "$id")]
    public string id { get; set; }
    [JsonProperty(PropertyName = "$values")]
    public List<object> values { get; set; }
}

public class RootObject
{
    [JsonProperty(PropertyName = "$id")]
    public string id { get; set; }
    public bool success { get; set; }
    public Errors errors { get; set; }
}

答案 2 :(得分:0)

您需要您的对象属性来匹配json字符串($ id而不是_invalid_name_ $ id),然后您可以使用:

JsonConvert.DeserializeObject<RootObject>(jsonString);

答案 3 :(得分:0)

这是一个简单的类,用于将json字符串从对象或对象(T)序列化。可以反序列化对象的数组(列表)。

public class HelperSerializer<T> where T: class
{ 
    public static string WriteFromObject(T source)
    {
        using (var ms = new MemoryStream())            {  
            var ser = new DataContractJsonSerializer(typeof(T));
            ser.WriteObject(ms, source);
            byte[] json = ms.ToArray();
            return Encoding.UTF8.GetString(json, 0, json.Length);
        }
    }

    // Deserialize a JSON stream to an object.  
    public static T ReadToObject(string json)
    {          

        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            var ser = new DataContractJsonSerializer(typeof(T));
            return ser.ReadObject(ms) as T;
        }
    }

}

使用persons = HelperSerializer<List<Person>>.ReadToObject(json); and var json = HelperSerializer<List<Person>>.WriteFromObject(persons);