读取 JSON,其中属性位于嵌套数组中,而不是对象的属性中



我正在使用newtonsoft.json在.net中解析此JSON,并使用 json2csharp.com 来获取我需要的类。

{
"rval": 0,
"msg_id": 3,
"param": [{ "camera_clock": "2015-09-03 04:42:20" },
          { "video_standard": "NTSC" },
          { "app_status": "idle" }
          // there are 30+ properties structured that way
         ]
}

json2csharp 给了我:

public int rval { get; set; }
public int msg_id { get; set; }
public List<Param> param { get; set; }
class Param 
{
     public string camera_clock {get;set;}
     public string video_standard {get;set;}
     public string app_status {get;set;}
     // 30+ more
}

Param对象包含所有参数。因此,每当我反序列化它时,我都会得到 31 个List<Param>对象,除了一个之外,所有属性均为空。我正在寻找的是获取一个包含所有 31 个属性集的 Param 对象。

不幸的是,我无法将 JSON 格式更改为以下内容(这反映了我想如何阅读它):

"param": { 
   "camera_clock": "2015-09-03 04:42:20",
   "video_standard": "NTSC" ,
   "app_status": "idle" 
   // there are 30+ properties structured that way
 }

如果您希望Param对象具有一组与 JSON 中显示的属性数组相对应的固定属性集,则需要编写一个自定义JsonConverter,将属性从数组中冒泡到单个对象中。 因此:

public class ArrayToObjectConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(T).IsAssignableFrom(objectType);
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        if (existingValue == null)
        {
            var contract = serializer.ContractResolver.ResolveContract(objectType);
            existingValue = contract.DefaultCreator();
        }
        switch (reader.TokenType)
        {
            case JsonToken.StartArray:
                {
                    var jArray = JArray.Load(reader);
                    var jObj = new JObject();
                    foreach (var prop in jArray.OfType<JObject>().SelectMany(o => o.Properties()))
                        jObj.Add(prop);
                    using (var sr = jObj.CreateReader())
                    {
                        serializer.Populate(sr, existingValue);
                    }
                }
                break;
            case JsonToken.StartObject:
                serializer.Populate(reader, existingValue);
                break;
            default:
                var msg = "Unexpected token type " + reader.TokenType.ToString();
                Debug.WriteLine(msg);
                throw new JsonSerializationException(msg);
        }
        return existingValue;
    }
    public override bool CanWrite { get { return false; } }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

然后像这样使用它:

        var settings = new JsonSerializerSettings { Converters = new[] { new ArrayToObjectConverter<Param>() } };
        var root = JsonConvert.DeserializeObject<RootObject>(jsonString, settings);

请注意,我没有将(重新)序列化作为对象数组进行,因为您的问题没有要求它。

答案使用以下类定义:

public class Param
{
    public string camera_clock { get; set; }
    public string video_standard { get; set; }
    public string app_status { get; set; }
    public string stream_out_type { get; set; }
    public string save_low_resolution_clip { get; set; }
    public string video_resolution { get; set; }
    public string video_stamp { get; set; }
    public string video_quality { get; set; }
    public string timelapse_video { get; set; }
    public string photo_size { get; set; }
    public string photo_stamp { get; set; }
    public string photo_quality { get; set; }
    public string timelapse_photo { get; set; }
    public string selfie_photo { get; set; }
    public string burst_photo { get; set; }
    public string autoshoot_photo { get; set; }
    public string loop_record { get; set; }
    public string motion_detec_video { get; set; }
    public string status_led_switch { get; set; }
    public string wifi_led_switch { get; set; }
    public string osd_switch { get; set; }
    public string cardvr_switch { get; set; }
    public string delay_pwroff { get; set; }
    public string rotate_image { get; set; }
    public string mic_vol { get; set; }
    public string language { get; set; }
    public string date_disp_fmt { get; set; }
    public string auto_bkl_off { get; set; }
    public string auto_pwr_off { get; set; }
    public string light_freq { get; set; }
    public string meter_mode { get; set; }
    public string buzzer { get; set; }
}
public class RootObject
{
    public int rval { get; set; }
    public int msg_id { get; set; }
    public Param param { get; set; }
}

原型小提琴。

让我看看我是否理解了每件事,当你生成 C# 类时,你会得到类似的东西:

public class Param
{
    public string camera_clock { get; set; }
    public string video_standard { get; set; }
    public string app_status { get; set; }
    public string stream_out_type { get; set; }
    public string save_low_resolution_clip { get; set; }
    public string video_resolution { get; set; }
    public string video_stamp { get; set; }
    public string video_quality { get; set; }
    public string timelapse_video { get; set; }
    public string photo_size { get; set; }
    public string photo_stamp { get; set; }
    public string photo_quality { get; set; }
    public string timelapse_photo { get; set; }
    public string selfie_photo { get; set; }
    public string burst_photo { get; set; }
    public string autoshoot_photo { get; set; }
    public string loop_record { get; set; }
    public string motion_detec_video { get; set; }
    public string status_led_switch { get; set; }
    public string wifi_led_switch { get; set; }
    public string osd_switch { get; set; }
    public string cardvr_switch { get; set; }
    public string delay_pwroff { get; set; }
    public string rotate_image { get; set; }
    public string mic_vol { get; set; }
    public string language { get; set; }
    public string date_disp_fmt { get; set; }
    public string auto_bkl_off { get; set; }
    public string auto_pwr_off { get; set; }
    public string light_freq { get; set; }
    public string meter_mode { get; set; }
    public string buzzer { get; set; }
}
public class myClass
{
    public int rval { get; set; }
    public int msg_id { get; set; }
    public List<Param> param { get; set; }
}

之后您将使用NewtonSoft进行反序列化?

你必须做类似的事情

myClass deserializedProduct = JsonConvert.DeserializeObject<myClass>(output);

这是你想要的吗?

希望这个帮助

相关内容

  • 没有找到相关文章

最新更新