Newtonsoft.Json 空数组反序列化

tangxingangtom 2016-06-22 04:30:00
--下面是json

{
"user_idcard_suspicion": {
"idcard_with_other_names": [],
"idcard_with_other_phones": [],
"idcard_applied_in_orgs": []
}
}


phone_with_other_idcards 里面可能有参数,也可能没有,当没有的时候需要反序列化实体为null就好

正常情况下是这样的

{
"user_idcard_suspicion": {
"idcard_with_other_names": [
{
"susp_name": "吴小汉",
"susp_updt": "2015-11-09 19:25:04"
}
],
"idcard_with_other_phones": [
{
"susp_phone_province": "江苏",
"susp_phone_operator": "中国移动",
"susp_updt": "2015-11-02 10:31:39",
"susp_phone": "13773215030",
"susp_phone_city": "苏州"
}
],
"idcard_applied_in_orgs": [
{
"susp_org_type": "线上信用卡代还",
"susp_updt": "2015-10-23 16:31:00"
}
]
}
}


下面是实体

public HonpotUserIdcardSuspicion user_idcard_suspicion { get; set; }

public class HonpotUserIdcardSuspicion
{
/// <summary>
/// 用这个身份证号码绑定的其他姓名
/// </summary>
public List<HonpotIdcardWithOtherNames> idcard_with_other_names { get; set; }

/// <summary>
/// 用这个身份证绑定的其他手机号码
/// </summary>
public List<HonpotIdcardWithOtherPhones> idcard_with_other_phones { get; set; }

/// <summary>
/// 身份证在那些类型的机构中使用过
/// </summary>
public List<HonpotIdcardAppliedInOrgs> idcard_applied_in_orgs { get; set; }
}



JsonConvert.DeserializeObject<HoneypotSearch>(result); 使用的方法为这个



接口那边返回过来有时候有值,有时候没值,现没值的情况下报错
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'idcard_with_other_names'because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'bb', line 1, position 17.
...全文
621 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
我叫小菜菜 2016-06-23
  • 打赏
  • 举报
回复
更正一下: ...... 定义肯定是MYTYPE [] idcard_with_other_names; 如果是"idcard_with_other_names": [],反序列化后,idcard_with_other_names.length=0. 如果是"idcard_with_other_names": null,反序列化后,idcard_with_other_names=null? 所以使用时,
if(idcard_with_other_names!=null &&idcard_with_other_names.length>0)
即可。
我叫小菜菜 2016-06-23
  • 打赏
  • 举报
回复
根据文档自定义序列化格式
    public class MyJsonSerializerSettings
    {
        private MyJsonSerializerSettings() { }

        /// <summary>
        /// <para>json序列化的设置1</para>
        /// <para>DateFormatHandling.IsoDateFormat: writes dates in the ISO 8601 format, e.g. "2012-03-21T05:40Z".</para>
        /// <para>MissingMemberHandling:</para>
        /// <para>NullValueHandling:空值则忽略,不做json序列化</para>
        /// </summary>
        public static JsonSerializerSettings serSettings1
        {
            get
            {
                return new JsonSerializerSettings
                {
                    //DateFormatHandling = DateFormatHandling.IsoDateFormat,
                    MissingMemberHandling = MissingMemberHandling.Ignore,
                    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                    NullValueHandling = NullValueHandling.Ignore,
                    DefaultValueHandling = DefaultValueHandling.Include,
                    //ObjectCreationHandling = ObjectCreationHandling.Auto,
                    TypeNameHandling = TypeNameHandling.Auto 
                };
            }
        }
    }
这个 "idcard_with_other_names": [ { "susp_name": "吴小汉", "susp_updt": "2015-11-09 19:25:04" } ] 定义肯定是MYTYPE [] idcard_with_other_names; 如果是"idcard_with_other_names": [],反序列化后,idcard_with_other_names.length=0. 如果是"idcard_with_other_names": null,反序列化后,idcard_with_other_names.length=null? 所以使用时,if(idcard_with_other_names.length!=null &&idcard_with_other_names.length.length>0)即可。
tangxingangtom 2016-06-23
  • 打赏
  • 举报
回复
引用 5 楼 sp1234 的回复:
[quote=引用 楼主 tangxingangtom 的回复:] 接口那边返回过来有时候有值,有时候没值,现没值的情况下报错 Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'idcard_with_other_names'because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'bb', line 1, position 17.
这是你对数据处理的规则出错(也就是产生了什么“只有0个单元的数据集合)。这是你的处理代码有错,怎么能纠结到人家 json 上呢?[/quote] 他那边json确实没有问题,现在的问题是我这边反序列化json有问题,所以希望朋友给个解决方案
龍过鸡年 2016-06-22
  • 打赏
  • 举报
回复
使用 JsonSerializerSettings 的 NullValueHandling 设置 JsonConvert.DeserializeObject(s, new JsonSerializerSettings { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore }); NullValueHandling setting http://www.newtonsoft.com/json/help/html/NullValueHandlingIgnore.htm 序列化时,如果字段或属性为 null,则不对其序列化。估计就是这个样子的 { "user_idcard_suspicion": { } } 反序列化时,如果没有发现该字段或属性,则设置为 null。
showjim 2016-06-22
  • 打赏
  • 举报
回复
试试fastCSharp
            HoneypotSearch value = fastCSharp.emit.jsonParser.Parse<HoneypotSearch>(result);
  • 打赏
  • 举报
回复
引用 楼主 tangxingangtom 的回复:
接口那边返回过来有时候有值,有时候没值,现没值的情况下报错 Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'idcard_with_other_names'because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'bb', line 1, position 17.
这是你对数据处理的规则出错(也就是产生了什么“只有0个单元的数据集合)。这是你的处理代码有错,怎么能纠结到人家 json 上呢?
  • 打赏
  • 举报
回复
数组有0个单元,跟 null 完全是两个概念。你应该先把你的源对象中的数组改为null,然后再 sjon 序列化。
tangxingangtom 2016-06-22
  • 打赏
  • 举报
回复
#region GetHoneypotSearchJson public static string GetHoneypotSearchJson(string result) { result = result.Replace("\"blacklist_category\":[],", ""); result = result.Replace("\"blacklist_details\":[],", ""); result = result.Replace("\"register_orgs\":[],", ""); result = result.Replace("\"phone_with_other_idcards\":[],", ""); result = result.Replace("\"phone_applied_in_orgs\":[],", ""); result = result.Replace("\"phone_with_other_names\":[],", ""); result = result.Replace("\"idcard_with_other_names\":[],", ""); result = result.Replace("\"idcard_with_other_phones\":[],", ""); result = result.Replace("\"idcard_applied_in_orgs\":[],", ""); return result; } #endregion 死办法解决了,希望能有更优化的解决方案
tangxingangtom 2016-06-22
  • 打赏
  • 举报
回复
这样肯定不行
全栈极简 2016-06-22
  • 打赏
  • 举报
回复
try{ //方案一序列化 }catch(Exception ex){ //方案二序列化 }

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧