相关疑难解决方法(0)

你如何使用 System.Text.Json 从一些 json 中读取一个简单的值?

我有这个 json

{"id":"48e86841-f62c-42c9-ae20-b54ba8c35d6d"}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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''
Run Code Online (Sandbox Code Playgroud)

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


编辑:

我刚刚发现我可以这样做:

Guid id = System.Text.Json.JsonDocument.Parse(json).RootElement.GetProperty("id").GetGuid();
Run Code Online (Sandbox Code Playgroud)

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

c# .net-core system.text.json

39
推荐指数
4
解决办法
3万
查看次数

JSON 属性到数组 C#

"inputs": {
    "input1": {
        "value": "abc"
    },
    "input2": {
        "value": "cde"
    },
    "input3": {
        "value": "efg"
    },
    "input4": {
        "value": "ghi"
    },      
}
Run Code Online (Sandbox Code Playgroud)

这里“输入”中的属性数量可能会有所不同。我怎样才能将其反序列化到类中:

class Inputs
{
    public Input[] Values{get; set;}
}

class Input
{
    public string input {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

一种选择是将 json“输入”更改为数组,但我现在没有这种选择

c# json json.net

4
推荐指数
1
解决办法
1925
查看次数

json反序列化为C#

对我的网络请求的响应如下(不在我的控制之下):

{
"nasdaq_imbalance": 
{
    "name": "nasdaq_imbalance", 
    "group": "Market Data", 
    "description": null
},
"DXOpen IM": 
{
    "name": "DXOpen IM", 
    "group": "Daily",
    "description": null
}, 
"Float Shares": 
{
    "name": "Float Shares", 
    "group": "Daily", 
    "description": null
}, 
Run Code Online (Sandbox Code Playgroud)

}

不知何故,我需要将它反序列化为包含对象列表的 C# 对象......基本上我需要一个这样的对象列表:

public class Dataset    {
    public string name { get; set; } 
    public string group { get; set; } 
    public string description { get; set; } 
}
Run Code Online (Sandbox Code Playgroud)

c# json converters deserialization

2
推荐指数
1
解决办法
126
查看次数

Noob C#类声明问题

所以我用json2csharp创建了一个类

    public class ResponseType
    {
        public class Query
        {
            public string q { get; set; }
            public object sku { get; set; }
            public int limit { get; set; }
            public object reference { get; set; }
            public object mpn_or_sku { get; set; }
            public string mpn { get; set; }
            public object brand { get; set; }
            public string __class__ { get; set; }
            public int start { get; set; }
            public object seller { get; set; }
        } …
Run Code Online (Sandbox Code Playgroud)

c# intellisense visual-studio

-1
推荐指数
1
解决办法
146
查看次数