http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3104|回复: 8

[咨询站长] 解析JSON到对象类为空的情况

[复制链接]
发表于 2015-12-9 17:54:32 | 显示全部楼层 |阅读模式
对象类代码:
namespace gys_auto
{
    public class purchasechanges
    {
        public int id { get; set; }
        public int supplierId { get; set; }
        public string supplierName { get; set; }
        public string sender { get; set; }
        public string code { get; set; }
        public int statusId { get; set; }
        public string amount { get; set; }
        public int receiverId { get; set; }
        public string receiverName { get; set; }
        public string receiverDate { get; set; }
        public string modifiedDate { get; set; }
        public IList<Detail> details { get; set; }
    }  

    public class Detail
    {
        public int departmentId { get; set; }
        public string departmentName { get; set; }
        public int receiverId { get; set; }
        public string receiverName { get; set; }
        public int materialId { get; set; }
        public string materialName { get; set; }
        public int unitId { get; set; }
        public string unitName { get; set; }
        public double quantity { get; set; }
        public string price { get; set; }
        public double amount { get; set; }
        public int unitIdOfOrder { get; set; }
        public string unitNameOfOrder { get; set; }
        public int quantityOfOrder { get; set; }
    }
}



JSON 字符:
[{"id":3,"supplierId":14,"supplierName":"蔬菜-孟锐","sender":0,"code":"SH150000003","statusId":3,"amount":14.0,"receiverId":100,"receiverName":"符思思","receiverDate":"2015-12-07 17:56:44","modifiedDate":"2015-12-07 17:56:44","details":[{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":10,"materialName":"大土豆","unitId":42,"unitName":"斤","quantity":2.0,"price":2.0,"amount":4.0,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":2.0},{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":9,"materialName":"高山娃娃菜","unitId":42,"unitName":"斤","quantity":2.0,"price":5.0,"amount":10.0,"unitIdOfOrder":4,"unitNameOfOrder":"包","quantityOfOrder":2.0}]}]


List<purchasechanges> jsonlist = (List<purchasechanges>)HttpHelper.JsonToObject<List<purchasechanges>>(retString.ToString());
生成后jsonlist为空,



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-12-9 18:04:27 | 显示全部楼层
不用那个json的dll?还要自己抽象?
 楼主| 发表于 2015-12-9 18:06:56 | 显示全部楼层
helloword 发表于 2015-12-9 18:04
不用那个json的dll?还要自己抽象?

json值是我通过别人的api调用获取到的,站长说每个都要加上""号,这个还要自己来处理一次了.
发表于 2015-12-9 18:07:20 | 显示全部楼层
[C#] 纯文本查看 复制代码
 StringBuilder sb = new StringBuilder();
            sb.Append("    {");
            sb.Append("        \"id\": \"3\",");
            sb.Append("        \"supplierId\": \"14\",");
            sb.Append("        \"supplierName\": \"蔬菜-孟锐\",");
            sb.Append("        \"sender\": \"0\",");
            sb.Append("        \"code\": \"SH150000003\",");
            sb.Append("        \"statusId\": \"3\",");
            sb.Append("        \"amount\": \"14\",");
            sb.Append("        \"receiverId\": \"100\",");
            sb.Append("        \"receiverName\": \"符思思\",");
            sb.Append("        \"receiverDate\": \"2015-12-07 17:56:44\",");
            sb.Append("        \"modifiedDate\": \"2015-12-07 17:56:44\",");
            sb.Append("        \"details\": [");
            sb.Append("            {");
            sb.Append("                \"departmentId\": \"110\",");
            sb.Append("                \"departmentName\": \"热菜\",");
            sb.Append("                \"receiverId\": \"100\",");
            sb.Append("                \"receiverName\": \"符思思\",");
            sb.Append("                \"materialId\": \"10\",");
            sb.Append("                \"materialName\": \"大土豆\",");
            sb.Append("                \"unitId\": \"42\",");
            sb.Append("                \"unitName\": \"斤\",");
            sb.Append("                \"quantity\": \"2\",");
            sb.Append("                \"price\": \"2\",");
            sb.Append("                \"amount\": \"4\",");
            sb.Append("                \"unitIdOfOrder\": \"42\",");
            sb.Append("                \"unitNameOfOrder\": \"斤\",");
            sb.Append("                \"quantityOfOrder\": \"2\"");
            sb.Append("            },");
            sb.Append("            {");
            sb.Append("                \"departmentId\": \"110\",");
            sb.Append("                \"departmentName\": \"热菜\",");
            sb.Append("                \"receiverId\": \"100\",");
            sb.Append("                \"receiverName\": \"符思思\",");
            sb.Append("                \"materialId\": \"9\",");
            sb.Append("                \"materialName\": \"高山娃娃菜\",");
            sb.Append("                \"unitId\": \"42\",");
            sb.Append("                \"unitName\": \"斤\",");
            sb.Append("                \"quantity\": \"2\",");
            sb.Append("                \"price\": \"5\",");
            sb.Append("                \"amount\": \"10\",");
            sb.Append("                \"unitIdOfOrder\": \"4\",");
            sb.Append("                \"unitNameOfOrder\": \"包\",");
            sb.Append("                \"quantityOfOrder\": \"2\"");
            sb.Append("            }");
            sb.Append("        ]");
            sb.Append("    }");
            
            purchasechanges jsonlist = (purchasechanges)HttpHelper.JsonToObject<purchasechanges>(sb.ToString());


最关键的问题是,你的明明就一个对象,你写成了集合,你Josn只有一个purchasechanges对象,集合应该是内部的Detail
发表于 2015-12-9 18:14:28 | 显示全部楼层
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CsharpHttpHelper;
using System.Text;

namespace CsharpHttpHelper_Demo
{
    public partial class Text : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("    {");
            sb.Append("        \"id\": \"3\",");
            sb.Append("        \"supplierId\": \"14\",");
            sb.Append("        \"supplierName\": \"蔬菜-孟锐\",");
            sb.Append("        \"sender\": \"0\",");
            sb.Append("        \"code\": \"SH150000003\",");
            sb.Append("        \"statusId\": \"3\",");
            sb.Append("        \"amount\": \"14\",");
            sb.Append("        \"receiverId\": \"100\",");
            sb.Append("        \"receiverName\": \"符思思\",");
            sb.Append("        \"receiverDate\": \"2015-12-07 17:56:44\",");
            sb.Append("        \"modifiedDate\": \"2015-12-07 17:56:44\",");
            sb.Append("        \"details\": [");
            sb.Append("            {");
            sb.Append("                \"departmentId\": \"110\",");
            sb.Append("                \"departmentName\": \"热菜\",");
            sb.Append("                \"receiverId\": \"100\",");
            sb.Append("                \"receiverName\": \"符思思\",");
            sb.Append("                \"materialId\": \"10\",");
            sb.Append("                \"materialName\": \"大土豆\",");
            sb.Append("                \"unitId\": \"42\",");
            sb.Append("                \"unitName\": \"斤\",");
            sb.Append("                \"quantity\": \"2\",");
            sb.Append("                \"price\": \"2\",");
            sb.Append("                \"amount\": \"4\",");
            sb.Append("                \"unitIdOfOrder\": \"42\",");
            sb.Append("                \"unitNameOfOrder\": \"斤\",");
            sb.Append("                \"quantityOfOrder\": \"2\"");
            sb.Append("            },");
            sb.Append("            {");
            sb.Append("                \"departmentId\": \"110\",");
            sb.Append("                \"departmentName\": \"热菜\",");
            sb.Append("                \"receiverId\": \"100\",");
            sb.Append("                \"receiverName\": \"符思思\",");
            sb.Append("                \"materialId\": \"9\",");
            sb.Append("                \"materialName\": \"高山娃娃菜\",");
            sb.Append("                \"unitId\": \"42\",");
            sb.Append("                \"unitName\": \"斤\",");
            sb.Append("                \"quantity\": \"2\",");
            sb.Append("                \"price\": \"5\",");
            sb.Append("                \"amount\": \"10\",");
            sb.Append("                \"unitIdOfOrder\": \"4\",");
            sb.Append("                \"unitNameOfOrder\": \"包\",");
            sb.Append("                \"quantityOfOrder\": \"2\"");
            sb.Append("            }");
            sb.Append("        ]");
            sb.Append("    }");
            
            purchasechanges jsonlist = (purchasechanges)HttpHelper.JsonToObject<purchasechanges>(sb.ToString());
        }
    }

    public class Detail
    {
        public int departmentId { get; set; }
        public string departmentName { get; set; }
        public int receiverId { get; set; }
        public string receiverName { get; set; }
        public int materialId { get; set; }
        public string materialName { get; set; }
        public int unitId { get; set; }
        public string unitName { get; set; }
        public int quantity { get; set; }
        public int price { get; set; }
        public int amount { get; set; }
        public int unitIdOfOrder { get; set; }
        public string unitNameOfOrder { get; set; }
        public int quantityOfOrder { get; set; }
    }

    public class purchasechanges
    {
        public int id { get; set; }
        public int supplierId { get; set; }
        public string supplierName { get; set; }
        public int sender { get; set; }
        public string code { get; set; }
        public int statusId { get; set; }
        public int amount { get; set; }
        public int receiverId { get; set; }
        public string receiverName { get; set; }
        public string receiverDate { get; set; }
        public string modifiedDate { get; set; }
        public IList<Detail> details { get; set; }
    }  
  
}

 楼主| 发表于 2015-12-10 10:47:31 | 显示全部楼层
[
{"id":3,"supplierId":14,"supplierName":"蔬菜-孟锐","sender":0,"code":"SH150000003","statusId":3,"amount":14.0,"receiverId":100,"receiverName":"符思思","receiverDate":"2015-12-07 17:56:44","modifiedDate":"2015-12-07 17:56:44","details":[{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":10,"materialName":"大土豆","unitId":42,"unitName":"斤","quantity":2.0,"price":2.0,"amount":4.0,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":2.0},{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":9,"materialName":"高山娃娃菜","unitId":42,"unitName":"斤","quantity":2.0,"price":5.0,"amount":10.0,"unitIdOfOrder":4,"unitNameOfOrder":"包","quantityOfOrder":2.0}]
},
{"id":4,"supplierId":14,"supplierName":"蔬菜-孟锐","sender":0,"code":"SH150000004","statusId":3,"amount":4.0,"receiverId":100,"receiverName":"符思思","receiverDate":"2015-12-08 09:27:40","modifiedDate":"2015-12-08 09:27:40",
"details":[{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":10,"materialName":"大土豆","unitId":42,"unitName":"斤","quantity":2.0,"price":2.0,"amount":4.0,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":2.0},{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":9,"materialName":"高山娃娃菜","unitId":42,"unitName":"斤","quantity":0.0,"price":5.0,"amount":0.0,"unitIdOfOrder":4,"unitNameOfOrder":"包","quantityOfOrder":0.0}]
},
{"id":11,"supplierId":14,"supplierName":"蔬菜-孟锐","sender":0,"code":"SH150000011","statusId":3,"amount":10.0,"receiverId":100,"receiverName":"符思思","receiverDate":"2015-12-08 10:28:24","modifiedDate":"2015-12-08 10:28:24","details":[{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":9,"materialName":"高山娃娃菜","unitId":42,"unitName":"斤","quantity":2.0,"price":5.0,"amount":10.0,"unitIdOfOrder":4,"unitNameOfOrder":"包","quantityOfOrder":2.0}]},{"id":13,"supplierId":12,"supplierName":"猪肉-段尚纯","sender":0,"code":"SH150000013","statusId":3,"amount":90.0,"receiverId":100,"receiverName":"符思思","receiverDate":"2015-12-08 14:38:10","modifiedDate":"2015-12-08 14:38:10","details":[{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":167,"materialName":"带皮五花肉","unitId":42,"unitName":"斤","quantity":0.0,"price":13.0,"amount":0.0,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":0.0},{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":170,"materialName":"猪头肉","unitId":42,"unitName":"斤","quantity":18.0,"price":5.0,"amount":90.0,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":18.0}]},{"id":16,"supplierId":12,"supplierName":"猪肉-段尚纯","sender":0,"code":"SH150000016","statusId":3,"amount":143.0,"receiverId":100,"receiverName":"符思思","receiverDate":"2015-12-08 14:47:43","modifiedDate":"2015-12-08 14:47:43","details":[{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":167,"materialName":"带皮五花肉","unitId":42,"unitName":"斤","quantity":11.0,"price":13.0,"amount":143.0,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":11.0}]},{"id":19,"supplierId":12,"supplierName":"猪肉-段尚纯","sender":0,"code":"SH150000019","statusId":3,"amount":65.0,"receiverId":100,"receiverName":"符思思","receiverDate":"2015-12-08 15:02:21","modifiedDate":"2015-12-08 15:02:21","details":[{"departmentId":110,"departmentName":"热菜","receiverId":100,"receiverName":"符思思","materialId":167,"materialName":"带皮五花肉","unitId":42,"unitName":"斤","quantity":5.0,"price":13.0,"amount":65.0,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":5.0}]},{"id":21,"supplierId":14,"supplierName":"蔬菜-孟锐","sender":0,"code":"SH150000021","statusId":3,"amount":10.65,"receiverId":100,"receiverName":"符思思","receiverDate":"2015-12-08 15:29:29","modifiedDate":"2015-12-08 15:29:29","details":[{"departmentId":109,"departmentName":"厨房","receiverId":100,"receiverName":"符思思","materialId":8,"materialName":"香菜","unitId":42,"unitName":"斤","quantity":0.5,"price":4.5,"amount":2.25,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":0.5},{"departmentId":109,"departmentName":"厨房","receiverId":100,"receiverName":"符思思","materialId":13,"materialName":"尖青椒","unitId":42,"unitName":"斤","quantity":3.0,"price":2.8,"amount":8.4,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":3.0},{"departmentId":109,"departmentName":"厨房","receiverId":100,"receiverName":"符思思","materialId":42,"materialName":"凤尾菌","unitId":42,"unitName":"斤","quantity":0.0,"price":4.0,"amount":0.0,"unitIdOfOrder":42,"unitNameOfOrder":"斤","quantityOfOrder":0.0}]}]

当JSON格式为这样时候,又是空了.
 楼主| 发表于 2015-12-10 11:09:24 | 显示全部楼层
{
    "dslist": [
        {
            "id": 3,
            "supplierId": 14,
            "details": [
                {
                    "departmentId": 110,
                    "departmentName": "热菜"
                },
                {
                    "departmentId": 110,
                    "departmentName": "热菜"
                }
            ]
        },
        {
            "id": 4,
            "supplierId": 14,
            "details": [
                {
                    "departmentId": 110,
                    "departmentName": "热菜"
                },
                {
                    "departmentId": 110,
                    "departmentName": "热菜"
                }
            ]
        },
        {
            "id": 11,
            "supplierId": 14,
            "details": [
                {
                    "departmentId": 110,
                    "departmentName": "热菜"
                }
            ]
        },
        {
            "id": 13,
            "supplierId": 12,
            "details": [
                {
                    "departmentId": 110,
                    "departmentName": "热菜"
                },
                {
                    "departmentId": 110,
                    "departmentName": "热菜"
                }
            ]
        },
        {
            "id": 21,
            "supplierId": 14,
            "details": [
                {
                    "departmentId": 109,
                    "departmentName": "厨房"
                },
                {
                    "departmentId": 109,
                    "departmentName": "厨房"
                }
            ]
        }
    ]
}



namespace gys_auto
{
    public class purchasechangeslist
    {
        public IList<Dslistlist> dslist { get; set; }
    }

    public class Detaillist
    {
        public string departmentId { get; set; }
        public string departmentName { get; set; }
        public string receiverId { get; set; }
        public string receiverName { get; set; }
        public string materialId { get; set; }
        public string materialName { get; set; }
        public string unitId { get; set; }
        public string unitName { get; set; }
        public string quantity { get; set; }
        public string price { get; set; }
        public string amount { get; set; }
        public string unitIdOfOrder { get; set; }
        public string unitNameOfOrder { get; set; }
        public string quantityOfOrder { get; set; }
    }

    public class Dslistlist
    {
        public string id { get; set; }
        public string supplierId { get; set; }
        public string supplierName { get; set; }
        public string sender { get; set; }
        public string code { get; set; }
        public string statusId { get; set; }
        public string amount { get; set; }
        public string receiverId { get; set; }
        public string receiverName { get; set; }
        public string receiverDate { get; set; }
        public string modifiedDate { get; set; }
        public IList<Detaillist> details { get; set; }
    }

}






List<purchasechangeslist> jsonlistview = (List<purchasechangeslist>)HttpHelper.JsonToObject<List<purchasechangeslist>>(retString.ToString());
发表于 2015-12-10 15:53:20 | 显示全部楼层
http://tool.sufeinet.com/Creater ... 6fd3c1c7d834034c686 重新生成一下,因为这样你的Json的Dslistlist就不再是一个对象,而是一个集合,看我链接里生成的。
发表于 2015-12-10 15:54:55 | 显示全部楼层
新生成的这个是会兼容前一个的,可以都使用这一种方法处理,加一个比删除一个好处理,前面加上{"list": 后面加上}就行了
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-2 19:40

© 2014-2021

快速回复 返回顶部 返回列表