关于返回多维数组的json解析的问题

.Net技术 码拜 8年前 (2016-09-26) 903次浏览
[{“id”:”6664″,”userID”:”830″,”oid”:”14731485569990″,”integralAll”:”0″,”integral”:”-0.01″,”sourceType”:”1″,”addtime”:”1473148650″,”sourceID”:””,”sHash”:””,”coid”:”0″,”isshow”:”1″,”systemid”:”34″,”type”:”1″,”pid”:”0″,”yvip”:”0″,”status”:”1″,”catid”:”2″,”sharetime”:””,”shareurl”:””,”tuangou”:””,”viplast_time”:”0″,”course”:[{“id”:”77″,”listorder”:”0″,”updatetime”:”1461222523″,”inputtime”:”1461222523″,”ctitle”:”QQ群提取好友”,”style”:””,”choice_kc”:[{“id”:”262″,”name”:”QQ群提取好友”,”sort”:”0″,”mv_url”:”d9a628711e944cc26c0073ee8f2d348c_d”},{“id”:”290″,”name”:”天涯论坛顶帖机”,”sort”:”0″,”mv_url”:”d9a628711e6c30477b9877decf2fae1d_d”},{“id”:”291″,”name”:”豆瓣顶帖机”,”sort”:”0″,”mv_url”:”d9a628711ed9512ab7f1d6ec23e137e1_d”},{“id”:”292″,”name”:”猫扑回帖助手”,”sort”:”0″,”mv_url”:”d9a628711ef83a2d2294e7bbe5d978da_d”},{“id”:”293″,”name”:”百度贴吧帖子采集器”,”sort”:”0″,”mv_url”:”d9a628711e80d534834f78d6e2351407_d”}],”choice_kc_idstr”:”262,290,291,292,293″,”systemid”:”34″,”thumb”:”/var/upload/image/2016/05/20160503160613_55082.jpg”,”introduce”:””},{“id”:”87″,”listorder”:”0″,”updatetime”:”1470129274″,”inputtime”:”1470129274″,”ctitle”:”章节2测试”,”style”:””,”choice_kc”:[{“id”:”298″,”name”:”企业采集器”,”sort”:”0″,”mv_url”:”d9a628711e3378efe26effc9c5514013_d”},{“id”:”299″,”name”:”问问精准关键词用户提取”,”sort”:”0″,”mv_url”:”d9a628711e3b04b17bef9f10b6d4e166_d”},{“id”:”303″,”name”:”慧聪企业信息采集”,”sort”:”0″,”mv_url”:”d9a628711e4172a0faf5863ea1ac3d75_d”},{“id”:”304″,”name”:”生意宝”,”sort”:”0″,”mv_url”:”d9a628711e1359778211c64fcf070ba2_d”},{“id”:”305″,”name”:”新浪微博点赞评论”,”sort”:”0″,”mv_url”:”d9a628711e5c71039e4f435d78a7cd6b_d”},{“id”:”306″,”name”:”IM”,”sort”:”0″,”mv_url”:”d9a628711e85bebac2a72eeae807b0cd_d”},{“id”:”307″,”name”:”图片转换工具”,”sort”:”0″,”mv_url”:”d9a628711e44758f1a33eec29d8813f3_d”}],”choice_kc_idstr”:”298,299,303,304,305,306,307″,”systemid”:”34″,”thumb”:”/var/upload/image/2016/08/20160802171452_94984.jpg”,”introduce”:””}],”sysinfo”:[{“id”:”34″,”stitle”:”QQ群提取好友”,”thumb”:”/var/upload/image/2016/05/20160503160525_60177.jpg”,”listorder”:”0″,”updatetime”:”1461219300″,”inputtime”:”1461219300″,”picture”:””,”introduce”:”QQ群提取好友一款可以批量提取qq群里面全部群成员QQ号码的软件。主要是帮助客户 获得精准潜在客户, 懂得QQ营销的朋友都明白,QQ群成员是精准的潜在客户.”,”exception”:”3,4″,”style”:””,”catid”:”2″,”couClass”:”8″,”sys_hours”:”3秒29″,”keyword”:””,”description”:””,”l_nanyi”:”1″,”l_jiage”:”5″,”keshi”:”1″}]}]
这是返回的嵌套数组,现在解析的时候几种方法都没搞定,哪位大师帮本人看看
public class Q_message
{
public Tx_message Tx_message = new Tx_message();
public Tx_Qmg Tx_Qmg = new Tx_Qmg();
}
public string DwJsonMy(string json)
{
//string ss = json;
            //Q_message result = JsonConvert.DeserializeObject<Q_message>(json);
Q_message user = (Q_message)JsonConvert.DeserializeObject(json, typeof(Q_message));

//var tx_mg = JsonConvert.DeserializeObject<List<Q_message>>(json);
List<Q_message> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Q_message>>(json);

return json;
}
标红的返回的错误是“Newtonsoft.Json.JsonSerializationException”类型的未经处理的异常在 Newtonsoft.Json.dll 中发生
其他信息: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type “SzzPc.Cls_Rootpath+Q_message” 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 “”, line 1, position 1.
list没有报错,但是返回的数据都为空

解决方案

1

先构造类啊.

public class ChoiceKc
{
    public string id { get; set; }
    public string name { get; set; }
    public string sort { get; set; }
    public string mv_url { get; set; }
}
public class Course
{
    public string id { get; set; }
    public string listorder { get; set; }
    public string updatetime { get; set; }
    public string inputtime { get; set; }
    public string ctitle { get; set; }
    public string style { get; set; }
    public List<ChoiceKc> choice_kc { get; set; }
    public string choice_kc_idstr { get; set; }
    public string systemid { get; set; }
    public string thumb { get; set; }
    public string introduce { get; set; }
}
public class Sysinfo
{
    public string id { get; set; }
    public string stitle { get; set; }
    public string thumb { get; set; }
    public string listorder { get; set; }
    public string updatetime { get; set; }
    public string inputtime { get; set; }
    public string picture { get; set; }
    public string introduce { get; set; }
    public string exception { get; set; }
    public string style { get; set; }
    public string catid { get; set; }
    public string couClass { get; set; }
    public string sys_hours { get; set; }
    public string keyword { get; set; }
    public string description { get; set; }
    public string l_nanyi { get; set; }
    public string l_jiage { get; set; }
    public string keshi { get; set; }
}
public class RootObject
{
    public string id { get; set; }
    public string userID { get; set; }
    public string oid { get; set; }
    public string integralAll { get; set; }
    public string integral { get; set; }
    public string sourceType { get; set; }
    public string addtime { get; set; }
    public string sourceID { get; set; }
    public string sHash { get; set; }
    public string coid { get; set; }
    public string isshow { get; set; }
    public string systemid { get; set; }
    public string type { get; set; }
    public string pid { get; set; }
    public string yvip { get; set; }
    public string status { get; set; }
    public string catid { get; set; }
    public string sharetime { get; set; }
    public string shareurl { get; set; }
    public string tuangou { get; set; }
    public string viplast_time { get; set; }
    public List<Course> course { get; set; }
    public List<Sysinfo> sysinfo { get; set; }
}

不知道对不对 试试

4

题主的定义与 JSON 不匹配

        public class Tx_message
        {
            public string id { get; set; }
            public string userID { get; set; }
            public string oid { get; set; }
            public string integralAll { get; set; }
            public string integral { get; set; }
            public string sourceType { get; set; }
            public string addtime { get; set; }
            public string sourceID { get; set; }
            public string sHash { get; set; }
            public string coid { get; set; }
            public string isshow { get; set; }
            public string systemid { get; set; }
            public string type { get; set; }
            public string pid { get; set; }
            public string yvip { get; set; }
            public string status { get; set; }
            public string catid { get; set; }
            public string sharetime { get; set; }
            public string shareurl { get; set; }
            public string tuangou { get; set; }
            public string viplast_time { get; set; }
            public courses[] course { get; set; }
            public Tx_Qmg[] sysinfo { get; set; }
        }
        public class courses
        {
            public string id { get; set; }
            public string listorder { get; set; }
            public string updatetime { get; set; }
            public string inputtime { get; set; }
            public string ctitle { get; set; }
            public string style { get; set; }
            public choice_kc[] choice_kc { get; set; }
            public string choice_kc_idstr { get; set; }
            public string systemid { get; set; }
            public string thumb { get; set; }
            public string introduce { get; set; }
        }
        public class choice_kc
        {
            public string id { get; set; }
            public string name { get; set; }
            public string sort { get; set; }
            public string mv_url { get; set; }
        }
        public class Tx_Qmg
        {
            public string id { get; set; }
            public string stitle { get; set; }
            public string thumb { get; set; }
            public string listorder { get; set; }
            public string updatetime { get; set; }
            public string inputtime { get; set; }
            public string picture { get; set; }
            public string introduce { get; set; }
            public string exception { get; set; }
            public string style { get; set; }
            public string catid { get; set; }
            public string couClass { get; set; }
            public string sys_hours { get; set; }
            public string keyword { get; set; }
            public string description { get; set; }
            public string l_nanyi { get; set; }
            public string l_jiage { get; set; }
            public string keshi { get; set; }
        }

这样使用fastCSharp没有问题

            Tx_message[] result = fastCSharp.emit.jsonParser.Parse<Tx_message[]>(json);

15

你是不是属性名没有和json里的Key对应起来?把你现在的实体类贴出来

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于返回多维数组的json解析的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)