DeSerializing JSON返回null C#

时间:2016-11-25 09:24:36

标签: c# json json.net deserialization

我遵循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; }
}

在我反序列化时,我没有收到任何错误。我也可以轻松浏览。

但是当我试图获得element [n]的值时(output_parameters [0] .value.array_.element [0] .value)。它返回null。

这是什么问题?

注意:我只需要从JSON中获取少量值。例如,我不需要像这样的价值观          "类型":"串&#34 ;,          "名称":"错误&#34 ;,          "范围":"本地" 这就是我创建像这样的C#类的原因。

1 个答案:

答案 0 :(得分:3)

您的类应该如下所示,因为output-parameters是一个数组[,它可能包含更多值或列表,因此创建一个包含输出参数列表的Rootobject

现在output-parameters不仅包含valuename,还包含typescopestringSomeString位于同一层次结构中。

您没有声明名为SomeString1的节点,此处称为stringSomeString1

您的类值还包含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);

add_action( 'wp_router_generate_routes', 'bl_add_routes', 20 );

function bl_add_routes( $router ) { 
    $route_args = array(
                        'path' => '^new-demo-route',
                        'page_callback' => 'my_callback'
                );

    $router->add_route( 'demo-route-id', $route_args );
}

function my_callback( ) {
    return "Congrats! Your demo callback is fully functional. Now make it do something fancy";
}

然后你应该使用以下代码反序列化它

curl my-domain/new-demo-route?param_key=param_value -d "{ctn: 'here is body'}"

屏幕截图显示已提取的值

ScreenShot