首页 > 解决方案 > 使用c#解析json时出错

问题描述

我试着写了一个程序分别获取多个指定的内容,但是用前面的代码解析的时候出现了错误信息这是原始的json数据内容:

{
    "code": 0,
    "count": 1,
    "data": [
        {
            "id": 1,
            "last_login_time": "2020-10-03",
            "create_time": "2020-10-02",
            "update_time": null,
        }
    ],
    "searchD": {
        "phone": "0123456789"
    }
}

我使用下面的代码来获取last_login_time, create_time, update_time, id 但是产生的错误无法理解甚至无法解决这是我的代码:</p>

var json = "{\"code\": 0,\"count\": 1,\"data\": [{\"id\": 1,\"last_login_time\": \"2020-10-03\",\"create_time\": \"2020-10-02\",\"update_time\": null,}],\"searchD\": {\"phone\": \"0123456789\"}}";

            OrderDetails[] datas = JsonConvert.DeserializeObject<OrderDetails[]>(json);
            List<OrderDetailsInsert> insert = new List<OrderDetailsInsert>();
            foreach (OrderDetails data in datas)
            {
                foreach (var item in data.goods)
                {
                    OrderDetailsInsert getinfo = new OrderDetailsInsert();
                    Console.WriteLine(getinfo.id = item.id);
                }

            }
public class OrderDetailsInsert
        {
            public int id { get; set; }
            public string phone { get; set; }
            public string phoneAddr { get; set; }
            public object code { get; set; }
            public object name { get; set; }
            public string is_vip { get; set; }
            public object birthday { get; set; }
            public object idcard { get; set; }
            public object city { get; set; }
            public object is_credit_card { get; set; }
            public object is_room { get; set; }
            public object is_security { get; set; }
            public object is_gold { get; set; }
            public object is_vehicle { get; set; }
            public object income { get; set; }
            public object is_insurance { get; set; }
            public object check_in_time { get; set; }
            public object loan_month { get; set; }
            public int gold_count { get; set; }
            public int channel_id { get; set; }
            public string channel_code { get; set; }
            public string channel_name { get; set; }
            public string channel_status { get; set; }
            public int zhima { get; set; }
            public int play_money { get; set; }
            public int credit_card_use_money { get; set; }
            public int s_year_overdue_loan { get; set; }
            public int score { get; set; }
            public object open_bank { get; set; }
            public object phone_auth_time { get; set; }
            public string client { get; set; }
            public object bankcard { get; set; }
            public string vTime { get; set; }
            public string last_login_time { get; set; }
            public string create_time { get; set; }
            public object update_time { get; set; }
            public object is_sign { get; set; }
            public object platform { get; set; }
            public object rights_pay_orderid { get; set; }
            public string phoneMd5 { get; set; }
            public string orderNo { get; set; }
            public string vipPrice { get; set; }
            public string vipS { get; set; }
            public string vipE { get; set; }
            public string payTime { get; set; }
            public string dingzhi { get; set; }
            public int sqs { get; set; }
            public string show_phone { get; set; }
            public string channelStatus { get; set; }
        }

        //1
        public class OrderDetails
        {
            public GoodsInfoList[] goods { get; set; }
        }


        //2
        public class GoodsInfoList
        {
            public int id { get; set; }
            public string phone { get; set; }
            public string phoneAddr { get; set; }
            public object code { get; set; }
            public object name { get; set; }
            public string is_vip { get; set; }
            public object birthday { get; set; }
            public object idcard { get; set; }
            public object city { get; set; }
            public object is_credit_card { get; set; }
            public object is_room { get; set; }
            public object is_security { get; set; }
            public object is_gold { get; set; }
            public object is_vehicle { get; set; }
            public object income { get; set; }
            public object is_insurance { get; set; }
            public object check_in_time { get; set; }
            public object loan_month { get; set; }
            public int gold_count { get; set; }
            public int channel_id { get; set; }
            public string channel_code { get; set; }
            public string channel_name { get; set; }
            public string channel_status { get; set; }
            public int zhima { get; set; }
            public int play_money { get; set; }
            public int credit_card_use_money { get; set; }
            public int s_year_overdue_loan { get; set; }
            public int score { get; set; }
            public object open_bank { get; set; }
            public object phone_auth_time { get; set; }
            public string client { get; set; }
            public object bankcard { get; set; }
            public string vTime { get; set; }
            public string last_login_time { get; set; }
            public string create_time { get; set; }
            public object update_time { get; set; }
            public object is_sign { get; set; }
            public object platform { get; set; }
            public object rights_pay_orderid { get; set; }
            public string phoneMd5 { get; set; }
            public string orderNo { get; set; }
            public string vipPrice { get; set; }
            public string vipS { get; set; }
            public string vipE { get; set; }
            public string payTime { get; set; }
            public string dingzhi { get; set; }
            public int sqs { get; set; }
            public string show_phone { get; set; }
            public string channelStatus { get; set; }

        }

错误:

Newtonsoft.Json.JsonSerializationException:“Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'ConsoleApp2.Program+OrderDetails[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'code', line 1, position 8.”

有没有办法分别获取last_login_time、create_time、update_time、id?

标签: c#json.netvisual-studio

解决方案


您可以定义以下类来匹配您的 json 字符串。

public class Datum
    {
        public int id { get; set; }
        public string last_login_time { get; set; }
        public string create_time { get; set; }
        public object update_time { get; set; }
    }

    public class SearchD
    {
        public string phone { get; set; }
    }

    public class Root
    {
        public int code { get; set; }
        public int count { get; set; }
        public List<Datum> data { get; set; }
        public SearchD searchD { get; set; }
    }

然后您可以尝试以下代码来获取所需的信息。

var json = "{\"code\": 0,\"count\": 1,\"data\": [{\"id\": 1,\"last_login_time\": \"2020-10-03\",\"create_time\": \"2020-10-02\",\"update_time\": null}],\"searchD\": {\"phone\": \"0123456789\"}}";
            Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(json);
            foreach (var item in myDeserializedClass.data)
            {
                Console.WriteLine(item.create_time);
                Console.WriteLine(item.id);
                Console.WriteLine(item.last_login_time);
                Console.WriteLine(item.update_time);
            }

结果:

在此处输入图像描述


推荐阅读