DeSerializing JSON返回null C#

hi3rlvi2  于 5个月前  发布在  C#
关注(0)|答案(2)|浏览(71)

我有以下JSON,我正尝试反序列化。

{  
"output-parameters":[  
  {  
     "value":{  
        "array":{  
           "elements":[  
              {  
                 "string":{  
                    "value":"cp-100"
                 }
              },
              {  
                 "string":{  
                    "value":"cp-101"
                 }
              },
              {  
                 "string":{  
                    "value":"cp-100"
                 }
              },
              {  
                 "string":{  
                    "value":"cp-101"
                 }
              },
              {  
                 "string":{  
                    "value":"cp-100"
                 }
              }
           ]
        }
     },
     "type":"Array/string",
     "name":"Tags",
     "scope":"local"
  },
  {  
     "value":{  
        "string":{  
           "value":"subscribed"
        }
     },
     "type":"string",
     "name":"Error",
     "scope":"local"
  }
]
}

字符串
我已经创建了以下类来绑定JSON

public class OutputParameter
{
    [JsonProperty(PropertyName = "value")]
    public value value { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string name { get; set; }
}

public class value
{
    [JsonProperty(PropertyName = "array")]
    public array_ array_ { get; set; }
}

public class array_
{
    [JsonProperty(PropertyName = "elements")]
    public element[] element { get; set; }
}

public class element
{
    [JsonProperty(PropertyName = "value")]
    public string value { get; set; }
}


我没有得到任何错误,而我正在反序列化它。而且我可以很容易地通过导航。
但是当i,m试图获取元素[n]的值(output_parameters[0].value.array_.element[0].value)时,它返回空值。
这里有什么问题吗?
注意事项:我只需要从JSON中获取一些值。例如,我不需要像“type”:“string”,“name”:“Error”,“scope”:“local”这样的值。这就是为什么我创建了这样的C#类。

tzcvj98z

tzcvj98z1#

你的类应该看起来像这样,因为output-parameters是一个数组[,它可能包含更多的值或列表,所以创建一个包含输出参数列表的Rootobject
现在output-parameters不仅包含valuename,而且还包含. typescope
没有名为string的节点的声明,在这里称为SomeStringSomeString1
您的类Value还包含string,此处表示为SomeString1

public class Rootobject
{
    [JsonProperty(PropertyName = "output-parameters")]
    public List<OutputParameters> outputparameters { get; set; }
}
public class OutputParameters
{
    [JsonProperty(PropertyName = "value")]
    public SomeValue value { get; set; }

    [JsonProperty(PropertyName = "type")]
    public string type { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string name { get; set; }

    [JsonProperty(PropertyName = "scope")]
    public string scope { get; set; }
}
public class SomeValue
{
    [JsonProperty(PropertyName = "array")]
    public SomeArray array { get; set; }

    [JsonProperty(PropertyName = "string")]
    public SomeString1 _string { get; set; }
}
public class SomeArray
{
    [JsonProperty(PropertyName = "elements")]
    public List<SomeElement> elements { get; set; }
}
public class SomeElement
{
    [JsonProperty(PropertyName = "string")]
    public SomeString _string { get; set; }
}
public class SomeString
{
    [JsonProperty(PropertyName = "value")]
    public string value { get; set; }
}
public class SomeString1
{
    [JsonProperty(PropertyName = "value")]
    public string value { get; set; }
}

字符串
然后,您应该使用以下代码将其转换为

Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(jsonstr);
Console.WriteLine(ro.outputparameters[0].value.array.elements[0]._string.value);


显示已提取值的屏幕截图


的数据

xoshrz7s

xoshrz7s2#

根据微软官方文档(https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/customize-properties?pivots=dotnet-8-0),“web默认命名策略是camel case”。

JsonSerializerOptions options = new(JsonSerializerDefaults.Web)
    {
        WriteIndented = true
    };

字符串
在.NET 8中,可以指定JSON在序列化/解序列化时可以使用的命名策略

var test  = new HttpResponseMessage
{
    Content = JsonContent.Create(jsonBody, options: new JsonSerializerOptions { DictionaryKeyPolicy = JsonNamingPolicy.CamelCase }),
    StatusCode = httpStatusCode
};

相关问题