C#-解析并读取具有重复键的JSON

时间:2018-11-15 17:25:55

标签: c# json parsing

我试图弄清楚如何解析下面的JSON并从中获取“文本”:“律师”。我看到它有许多分支。即数组和对象。我想在C#中做到这一点。这是JSON:

auto_now=True

1 个答案:

答案 0 :(得分:0)

首先使用Quicktype.io生成本机C#类:

class Customer(models.Model): 
    user = models.OneToOneField(User, blank=True, null=True, on_delete=models.CASCADE)  
    first_name = models.CharField(max_length=50, blank=True, null=True)
    last_name = models.CharField(max_length=150, blank=True, null=True)

然后,使用Newtonsoft deserialize the JSON到Result类的一个实例(我们叫它class PersonDetailForm(forms.ModelForm): class Meta: model = Customer fields = ( 'first_name', 'last_name' ) )。

然后您可以

public partial class Result
    {
        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("succeeded")]
        public bool Succeeded { get; set; }

        [JsonProperty("failed")]
        public bool Failed { get; set; }

        [JsonProperty("finished")]
        public bool Finished { get; set; }

        [JsonProperty("recognitionResult")]
        public RecognitionResult RecognitionResult { get; set; }
    }

    public partial class RecognitionResult
    {
        [JsonProperty("lines")]
        public Line[] Lines { get; set; }
    }

    public partial class Line
    {
        [JsonProperty("boundingBox")]
        public long[] BoundingBox { get; set; }

        [JsonProperty("text")]
        public string Text { get; set; }

        [JsonProperty("words")]
        public Word[] Words { get; set; }
    }

    public partial class Word
    {
        [JsonProperty("boundingBox")]
        public long[] BoundingBox { get; set; }

        [JsonProperty("text")]
        public string Text { get; set; }
    }

如果只想第一次出现,请使用result