51

我有这个json

{"id":"48e86841-f62c-42c9-ae20-b54ba8c35d6d"}

我该如何48e86841-f62c-42c9-ae20-b54ba8c35d6d摆脱它?我能找到的所有例子都显示做类似的事情

var o = System.Text.Json.JsonSerializer.Deserialize<some-type>(json);
o.id // <- here's the ID!

但是我没有适合这个定义的类型,我不想创建一个。我试过反序列化为动态,但我无法让它工作。

var result = System.Text.Json.JsonSerializer.Deserialize<dynamic>(json);
result.id // <-- An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Linq.Expressions.dll but was not handled in user code: ''System.Text.Json.JsonElement' does not contain a definition for 'id''

任何人都可以提出任何建议吗?


编辑:

我刚刚发现我可以做到这一点:

Guid id = System.Text.Json.JsonDocument.Parse(json).RootElement.GetProperty("id").GetGuid();

这确实有效 - 但有更好的方法吗?

4

7 回答 7

43

您可以反序列化为Dictionary

var dict = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(json)

或者只是反序列化Object将产生一个JsonElement你可以调用GetProperty的。

于 2019-08-28T22:24:49.440 回答
15

我最近将一个项目从 ASP.NET Core 2.2 迁移到 3,我遇到了这种不便。在我们的团队中,我们重视精益依赖,因此我们试图避免包含 Newtonsoft.JSON 并尝试使用System.Text.Json. 我们还决定不将大量 POCO 对象仅用于 JSON 序列化,因为我们的后端模型比 Web API 所需的更复杂。此外,由于非平凡的行为封装,后端模型不能轻易地用于序列化/反序列化 JSON 字符串。

我知道这System.Text.Json应该比 Newtonsoft.JSON 更快,但我相信这与特定 POCO 类的 ser/deser 有很大关系。无论如何,速度不在我们这个决定的利弊清单上,所以 YMMV。

长话短说,暂时我写了一个小的动态对象包装器,它JsonElement从 System.Text.Json 解包 s 并尝试尽可能地转换/转换。典型用法是将请求正文作为动态对象读取。同样,我很确定这种方法会扼杀任何速度提升,但这不是我们用例的关注点。

这是课程:

    public class ReflectionDynamicObject : DynamicObject {
        public JsonElement RealObject { get; set; }

        public override bool TryGetMember (GetMemberBinder binder, out object result) {
            // Get the property value
            var srcData = RealObject.GetProperty (binder.Name);

            result = null;

            switch (srcData.ValueKind) {
                case JsonValueKind.Null:
                    result = null;
                    break;
                case JsonValueKind.Number:
                    result = srcData.GetDouble ();
                    break;
                case JsonValueKind.False:
                    result = false;
                    break;
                case JsonValueKind.True:
                    result = true;
                    break;
                case JsonValueKind.Undefined:
                    result = null;
                    break;
                case JsonValueKind.String:
                    result = srcData.GetString ();
                    break;
                case JsonValueKind.Object:
                    result = new ReflectionDynamicObject {
                        RealObject = srcData
                    };
                    break;
                case JsonValueKind.Array:
                    result = srcData.EnumerateArray ()
                        .Select (o => new ReflectionDynamicObject { RealObject = o })
                        .ToArray ();
                    break;
            }

            // Always return true; other exceptions may have already been thrown if needed
            return true;
        }
    }

这是一个示例用法,用于解析请求主体 - 一部分位于我所有 WebAPI 控制器的基类中,它将主体公开为动态对象:

    [ApiController]
    public class WebControllerBase : Controller {

        // Other stuff - omitted

        protected async Task<dynamic> JsonBody () {
            var result = await JsonDocument.ParseAsync (Request.Body);
            return new ReflectionDynamicObject {
                RealObject = result.RootElement
            };
        }
    }

并且可以像这样在实际控制器中使用:

//[...]
    [HttpPost ("")]
    public async Task<ActionResult> Post () {
        var body = await JsonBody ();
        var name = (string) body.Name;
        //[...]
    }
//[...]

如果需要,您可以根据需要集成对 GUID 或其他特定数据类型的解析 - 而我们都在等待一些官方/框架认可的解决方案。

于 2019-11-03T11:59:02.117 回答
9

在usingJsonObject中添加了对 的支持。.NET 6System.Text.Json.Nodes

例子:

const string Json = "{\"MyNumber\":42, \"MyArray\":[10,11]}";
// dynamic
{
    dynamic obj = JsonNode.Parse(Json);
    int number = (int)obj["MyNumber"];
    Debug.Assert(number == 42);

    obj["MyString"] = "Hello";
    Debug.Assert((string)obj["MyString"] == "Hello");
}

// JsonObject 
{
    JsonObject obj = JsonNode.Parse(Json).AsObject();
    int number = (int)obj["MyNumber"];
    Debug.Assert(number == 42);
    
    obj["MyString"] = "Hello";
    Debug.Assert((string)obj["MyString"] == "Hello");
}

资料来源:

https://github.com/dotnet/runtime/issues/53195

https://github.com/dotnet/runtime/issues/45188

于 2021-11-10T13:34:16.047 回答
6

在 System.Text.Json (.NET Core 3+) 中解析字符串的实际方法

        var jsonStr = "{\"id\":\"48e86841-f62c-42c9-ae20-b54ba8c35d6d\"}";
        using var doc = JsonDocument.Parse(jsonStr);
        var root = doc.RootElement;
        var id = root.GetProperty("id").GetGuid();
于 2020-12-01T10:30:38.933 回答
1

您可以使用以下扩展方法来查询“xpath”之类的数据

public static string? JsonQueryXPath(this string value, string xpath, JsonSerializerOptions? options = null) => value.Deserialize<JsonElement>(options).GetJsonElement(xpath).GetJsonElementValue();

        
        public static JsonElement GetJsonElement(this JsonElement jsonElement, string xpath)
        {
            if (jsonElement.ValueKind is JsonValueKind.Null or JsonValueKind.Undefined)
                return default;

            string[] segments = xpath.Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);

            foreach (var segment in segments)
            {
                if (int.TryParse(segment, out var index) && jsonElement.ValueKind == JsonValueKind.Array)
                {
                    jsonElement = jsonElement.EnumerateArray().ElementAtOrDefault(index);
                    if (jsonElement.ValueKind is JsonValueKind.Null or JsonValueKind.Undefined)
                        return default;

                    continue;
                }

                jsonElement = jsonElement.TryGetProperty(segment, out var value) ? value : default;

                if (jsonElement.ValueKind is JsonValueKind.Null or JsonValueKind.Undefined)
                    return default;
            }

            return jsonElement;
        }

        public static string? GetJsonElementValue(this JsonElement jsonElement) => jsonElement.ValueKind != JsonValueKind.Null &&
                                                                                   jsonElement.ValueKind != JsonValueKind.Undefined
            ? jsonElement.ToString()
            : default;

使用简单如下

string raw = @"{
        ""data"": {
        ""products"": {
            ""edges"": [
                {
                    ""node"": {
                        ""id"": ""gid://shopify/Product/4534543543316"",
                        ""featuredImage"": {
                            ""originalSrc"": ""https://cdn.shopify.com/s/files/1/0286/pic.jpg"",
                            ""id"": ""gid://shopify/ProductImage/146345345339732""
                        }
                    }
                },
                {
                    ""node"": {
                        ""id"": ""gid://shopify/Product/123456789"",
                        ""featuredImage"": {
                            ""originalSrc"": ""https://cdn.shopify.com/s/files/1/0286/pic.jpg"",
                            ""id"": [
                                ""gid://shopify/ProductImage/123456789"",
                                ""gid://shopify/ProductImage/666666666""
                            ]
                        },
                        ""1"": {
                            ""name"": ""Tuanh""
                        }
                    }
                }
            ]
        }
        }
    }";

            System.Console.WriteLine(raw2.QueryJsonXPath("data.products.edges.0.node.featuredImage.id"));
于 2021-06-19T12:49:56.873 回答
-1

更新到 .NET Core 3.1 以支持

public static dynamic FromJson(this string json, JsonSerializerOptions options = null)
    {
        if (string.IsNullOrEmpty(json))
            return null;

        try
        {
            return JsonSerializer.Deserialize<ExpandoObject>(json, options);
        }
        catch
        {
            return null;
        }
    }
于 2019-12-07T08:31:51.060 回答
-1

您还可以将 json 反序列化为目标类的对象,然后照常读取其属性:

var obj = DeSerializeFromStrToObj<ClassToSerialize>(jsonStr);
Console.WriteLine($"Property: {obj.Property}");

哪里DeSerializeFromStrToObj是一个自定义类,它利用反射来实例化目标类的对象:

    public static T DeSerializeFromStrToObj<T>(string json)
    {
        try
        {
            var o = (T)Activator.CreateInstance(typeof(T));

            try
            {
                var jsonDict = JsonSerializer.Deserialize<Dictionary<string, string>>(json);

                var props = o.GetType().GetProperties();

                if (props == null || props.Length == 0)
                {
                    Debug.WriteLine($"Error: properties from target class '{typeof(T)}' could not be read using reflection");
                    return default;
                }

                if (jsonDict.Count != props.Length)
                {
                    Debug.WriteLine($"Error: number of json lines ({jsonDict.Count}) should be the same as number of properties ({props.Length})of our class '{typeof(T)}'");
                    return default;
                }

                foreach (var prop in props)
                {
                    if (prop == null)
                    {
                        Debug.WriteLine($"Error: there was a prop='null' in our target class '{typeof(T)}'");
                        return default;
                    }

                    if (!jsonDict.ContainsKey(prop.Name))
                    {
                        Debug.WriteLine($"Error: jsonStr does not refer to target class '{typeof(T)}'");
                        return default;
                    }

                    var value = jsonDict[prop.Name];
                    Type t = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                    object safeValue = value ?? Convert.ChangeType(value, t);
                    prop.SetValue(o, safeValue, null); // initialize property
                }
                return o;
            }
            catch (Exception e2)
            {
                Debug.WriteLine(e2.Message);
                return o;
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
            return default;
        }
    }

例如,您可以在此处测试您的 json

在这里,您可以找到一个完整的工作示例,其中包含您和/或未来读者可能感兴趣的不同序列化和反序列化方式:

using System;
using System.Collections.Generic;
using System.Text.Json;
using static Json_Tests.JsonHelpers;

namespace Json_Tests
{

public class Class1
{
    public void Test()
    {
        var obj1 = new ClassToSerialize();
        var jsonStr = obj1.ToString();

        // if you have the class structure for the jsonStr (for example, if you have created the jsonStr yourself from your code):
        var obj2 = DeSerializeFromStrToObj<ClassToSerialize>(jsonStr);
        Console.WriteLine($"{nameof(obj2.Name)}: {obj2.Name}");

        // if you do not have the class structure for the jsonStr (forexample, jsonStr comes from a 3rd party service like the web):
        var obj3 = JsonSerializer.Deserialize<object>(jsonStr) as JsonElement?;
        var propName = nameof(obj1.Name);
        var propVal1 = obj3?.GetProperty("Name");// error prone
        Console.WriteLine($"{propName}: {propVal1}");
        JsonElement propVal2 = default;
        obj3?.TryGetProperty("Name", out propVal2);// error prone
        Console.WriteLine($"{propName}: {propVal2}");

        var obj4 = DeSerializeFromStrToDict(jsonStr);
        foreach (var pair in obj4)
            Console.WriteLine($"{pair.Key}: {pair.Value}");
    }
}

[Serializable]
public class ClassToSerialize
{
    // important: properties must have at least getters
    public string Name { get; } = "Paul";
    public string Surname{ get; set; } = "Efford";

    public override string ToString() => JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
}

public static class JsonHelpers
{
    /// <summary>
    /// to use if you do not have the class structure for the jsonStr (forexample, jsonStr comes from a 3rd party service like the web)
    /// </summary>
    public static Dictionary<string, string> DeSerializeFromStrToDict(string json)
    {
        try
        {
            return JsonSerializer.Deserialize<Dictionary<string, string>>(json);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return new Dictionary<string, string>(); // return empty
        }
    }

    /// <summary>
    /// to use if you have the class structure for the jsonStr (for example, if you have created the jsonStr yourself from your code)
    /// </summary>
    public static T DeSerializeFromStrToObj<T>(string json) // see this: https://json2csharp.com/#
    {
        try
        {
            var o = (T)Activator.CreateInstance(typeof(T));

            try
            {
                var jsonDict = JsonSerializer.Deserialize<Dictionary<string, string>>(json);

                var props = o.GetType().GetProperties();

                if (props == null || props.Length == 0)
                {
                    Console.WriteLine($"Error: properties from target class '{typeof(T)}' could not be read using reflection");
                    return default;
                }

                if (jsonDict.Count != props.Length)
                {
                    Console.WriteLine($"Error: number of json lines ({jsonDict.Count}) should be the same as number of properties ({props.Length})of our class '{typeof(T)}'");
                    return default;
                }

                foreach (var prop in props)
                {
                    if (prop == null)
                    {
                        Console.WriteLine($"Error: there was a prop='null' in our target class '{typeof(T)}'");
                        return default;
                    }

                    if (!jsonDict.ContainsKey(prop.Name))
                    {
                        Console.WriteLine($"Error: jsonStr does not refer to target class '{typeof(T)}'");
                        return default;
                    }

                    var value = jsonDict[prop.Name];
                    Type t = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                    object safeValue = value ?? Convert.ChangeType(value, t);
                    prop.SetValue(o, safeValue, null); // initialize property
                }
                return o;
            }
            catch (Exception e2)
            {
                Console.WriteLine(e2.Message);
                return o;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return default;
        }
    }
}
}
于 2021-12-23T14:22:44.440 回答