C# 解析 Json - System.Runtime.Serialization.Json

发布于 2024-11-30 17:39:25 字数 1265 浏览 3 评论 0原文

我正在使用 System.Runtime.Serialization.Json 库

这不起作用

public class Detections
            {
                [DataContract]
                public class RootObject
                {
                    [DataMember(Name = "data")]
                    public DataObject Data { get; set; }
                }

                [DataContract]
                public class DataObject
                {
                    [DataMember(Name = "detections")]
                    public List<Detection> Detections { get; set; }
                }

                [DataContract]
                public class Detection
                {
                    [DataMember(Name = "language")]
                    public string Language { get; set; }
                    [DataMember(Name = "isReliable")]
                    public string IsReliable { get; set; }
                    [DataMember(Name = "confidence")]
                    public string Confidence { get; set; }
                }
            }

那么我应该如何解析它

{
     "data": {
      "detections": [
       [
        {
         "language": "tr",
         "isReliable": false,
         "confidence": 0.086520955
        }
       ]
      ]
     }
    }

I'm using the System.Runtime.Serialization.Json library

This doesn't Work

public class Detections
            {
                [DataContract]
                public class RootObject
                {
                    [DataMember(Name = "data")]
                    public DataObject Data { get; set; }
                }

                [DataContract]
                public class DataObject
                {
                    [DataMember(Name = "detections")]
                    public List<Detection> Detections { get; set; }
                }

                [DataContract]
                public class Detection
                {
                    [DataMember(Name = "language")]
                    public string Language { get; set; }
                    [DataMember(Name = "isReliable")]
                    public string IsReliable { get; set; }
                    [DataMember(Name = "confidence")]
                    public string Confidence { get; set; }
                }
            }

So how should I parse this

{
     "data": {
      "detections": [
       [
        {
         "language": "tr",
         "isReliable": false,
         "confidence": 0.086520955
        }
       ]
      ]
     }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁我热情 2024-12-07 17:39:25

出现此问题的原因有两个。

1) 需要从Detections 类中删除您的嵌套类。

2) JSON 包含一个用于检测成员的多维数组,我认为它需要是单一的。

我已经测试了下面详细的代码,它工作正常。

//Classes

[DataContract]
public class RootObject
{
    [DataMember(Name = "data")]
    public DataObject Data { get; set; }
}

[DataContract]
public class DataObject
{
    [DataMember(Name = "detections")]
    public List<Detection> Detections { get; set; }
}

[DataContract]
public class Detection
{
    [DataMember(Name = "language")]
    public string Language { get; set; }
    [DataMember(Name = "isReliable")]
    public string IsReliable { get; set; }
    [DataMember(Name = "confidence")]
    public string Confidence { get; set; }
}

//Code to deserialize

var serializer = new DataContractJsonSerializer(typeof(RootObject));
var json = "{\"data\": {\"detections\": [{\"language\": \"tr\",\"isReliable\": false,\"confidence\": 0.086520955}]}}";
var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
var rootObject = serializer.ReadObject(stream);
stream.Close();

我希望这对你有帮助。

The problem is occurring for two reasons.

1) Your nested classes needed to be removed from within the Detections class.

2) The JSON contains a multi-dimensional array for the detections member where I assume it needs to be single.

I have tested the code detailed below it works fine.

//Classes

[DataContract]
public class RootObject
{
    [DataMember(Name = "data")]
    public DataObject Data { get; set; }
}

[DataContract]
public class DataObject
{
    [DataMember(Name = "detections")]
    public List<Detection> Detections { get; set; }
}

[DataContract]
public class Detection
{
    [DataMember(Name = "language")]
    public string Language { get; set; }
    [DataMember(Name = "isReliable")]
    public string IsReliable { get; set; }
    [DataMember(Name = "confidence")]
    public string Confidence { get; set; }
}

//Code to deserialize

var serializer = new DataContractJsonSerializer(typeof(RootObject));
var json = "{\"data\": {\"detections\": [{\"language\": \"tr\",\"isReliable\": false,\"confidence\": 0.086520955}]}}";
var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
var rootObject = serializer.ReadObject(stream);
stream.Close();

I Hope this helps you.

财迷小姐 2024-12-07 17:39:25

在你的 json 中,“Detection”是一个列表列表。请注意,jdavies 的答案不承认这一点。

[DataContract]
public class DataObject
{
[DataMember(Name = "detections")]
public List<List<Detection>> Detections { get; set; }
}

In your json "Detection" is a list of lists. Please note that the answer from jdavies does not recognize this.

[DataContract]
public class DataObject
{
[DataMember(Name = "detections")]
public List<List<Detection>> Detections { get; set; }
}
贱人配狗天长地久 2024-12-07 17:39:25

除了 Detections 是集合的集合之外,一切都是正确的。我不知道为什么会这样,但它对我有用。

[DataContract]
public class DataObject
{
    [DataMember(Name = "detections")]
    public List<List<Detection>> Detections { get; set; }
}

Everything is correct except that Detections is a collection of collections. I don't know why it is that way but it works for me.

[DataContract]
public class DataObject
{
    [DataMember(Name = "detections")]
    public List<List<Detection>> Detections { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文