【问题标题】:An error occurred when parsing json using c#使用c#解析json时出错
【发布时间】:2020-10-02 19:00:27
【问题描述】:

我尝试写一个程序分别获取多个指定的内容,但是使用前面的代码解析时出现错误信息 这是原始的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 但是产生的错误无法理解甚至无法解决 这是我的代码:

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?

【问题讨论】:

  • 你能告诉我们错误是什么吗?在不知道错误信息是什么的情况下很难解决问题。
  • 错误信息可能是说我的内容不正确,代码无法解析这个json文本
  • “可能”不起作用。需要实际的错误消息/代码。请使用这些信息编辑您的原始帖子。
  • 已重新编辑并添加了错误信息
  • 您的 json 与您尝试将其反序列化的类不匹配。在您的 json 的顶层有 {},对吗?这意味着它是一个对象。您正在尝试将其反序列化为数组。 json 中的数组/列表由 [] 定义。此外,您尝试反序列化的 json 与您提供的类不匹配。例如。 OrderDetails 在 json 中没有名为“code”、“count”或“data”的属性。您应该从您的课程重新开始,并尝试使用一个干净的课程,您将一个接一个地添加到其中。这会让你明白问题所在。

标签: c# json .net visual-studio


【解决方案1】:

您可以定义以下类来匹配您的 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);
            }

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多