【问题标题】:System.Text.Json to Newtonsoft JsonSystem.Text.Json 到 Newtonsoft Json
【发布时间】:2020-09-04 02:11:36
【问题描述】:

newtonsoft Json 中是否有 JsonElement 和 JsonValueKind 等价物?将以下使用 System.Text.Json 的代码移植到 newtonsoft Json 的正确代码是什么?由于我的 dll 无法找到正确的 System.Buffers 程序集版本而导致我的移植的原因。我遵循了所有我能得到的建议,但我仍然无法解决它。所以想到了使用 Newtonsoft Json。

public static Dictionary<string, dynamic> JsonDeserialize(string Json)
    {
        JsonElement elm = JsonSerializer.Deserialize<JsonElement>(Json);
        Dictionary<string, dynamic> dict = ElementToDict(elm);
        return dict;
    }


public static dynamic ElementToDict(JsonElement obj)
{
    if (obj.ValueKind == JsonValueKind.Number)
    {
        return StringToDecimal(obj.GetRawText());
    }
    else if (obj.ValueKind == JsonValueKind.String)
    {
        return obj.GetString();
    }
    else if (obj.ValueKind == JsonValueKind.True || obj.ValueKind == JsonValueKind.False)
    {
        return obj.GetBoolean();
    }
    else if (obj.ValueKind == JsonValueKind.Object)
    {
        var map = obj.EnumerateObject().ToList();
        var newMap = new Dictionary<String, dynamic>();
        for (int i = 0; i < map.Count; i++)
        {
            newMap.Add(map[i].Name, ElementToDict(map[i].Value));
        }
        return newMap;
    }
    else if (obj.ValueKind == JsonValueKind.Array)
    {
        var items = obj.EnumerateArray().ToList();
        var newItems = new ArrayList();
        for (int i = 0; i < items.Count; i++)
        {
            newItems.Add(ElementToDict(obj[i]));
        }
        return newItems;
    }
    else
    {
        return null;
    }
}

【问题讨论】:

    标签: c# json.net


    【解决方案1】:

    您可以尝试使用JTokenJTokenType

    var tok = JsonConvert.DeserializeObject<JToken>("{\"test\": 1}"); // or JToken.Parse
    Console.WriteLine(tok.Type); // prints "Object"
    

    【讨论】:

    • 嗨,我在为这部分转换为 newtonsoft 时遇到了麻烦......你能帮忙吗? else if (obj.ValueKind == JsonValueKind.Object) { var map = obj.EnumerateObject().ToList(); var newMap = new Dictionary(); for (int i = 0; i
    • @AnsarBedharudeen 尝试调查LINQ to JSON
    • 感谢您的建议。我通过了文档。我未能实现的是“递归反序列化器”。函数 ElementToDict(JsonElement obj) 通过递归调用来实现这一点。我无法找到在 Newtonsoft Json 中实现 obj.EnumerateArray().ToList() 和 map[i].Name 的方法。请指教。
    • @AnsarBedharudeen obj.Children().ToList()?或this answer
    【解决方案2】:

    感谢@guru-stron,我成功移植到 Newtonsoft.Json。以下是我的代码:

    public static Dictionary<string, dynamic> JsonDeserialize(string Json)
            {
                Console.WriteLine(Json);
                var elm = JsonConvert.DeserializeObject<JToken>(Json);
                // Replace double with decimal in the map
                Dictionary<string, dynamic> dict = ElementToDict(elm);
                return dict;
            }
    
    
    public static dynamic ElementToDict(JToken obj)
            {
                
                if (obj.Type == JTokenType.Float || obj.Type == JTokenType.Integer)
                {
                    return StringToDecimal(obj.ToString());
                }
                else if (obj.Type == JTokenType.String)
                {
                    return obj.ToString();
                }
                else if (obj.Type == JTokenType.Boolean)
                {
                    return obj.ToObject<Boolean>();
                }
                else if (obj.Type == JTokenType.Object)
                {
                    var map = obj.Children().ToList();
                    var newMap = new Dictionary<String, dynamic>();
                    foreach (JProperty m in map)
                    {
                        newMap.Add(m.Name, ElementToDict(m.Value));
                    }
                    return newMap;
                }
                else if (obj.Type == JTokenType.Array)
                {
                    var items = obj.AsJEnumerable().ToList();
                    var newItems = new ArrayList();
                    for (int i = 0; i < items.Count; i++)
                    {
                        newItems.Add(ElementToDict(obj[i]));
                    }
                    return newItems;
                }
                else
                {
                    return null;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 2015-09-13
      • 1970-01-01
      相关资源
      最近更新 更多