【问题标题】:Newtonsoft JSON Deserialize problemeNewtonsoft JSON反序列化问题
【发布时间】:2020-04-03 01:29:43
【问题描述】:

当我进行休息 api 调用时,我有以下 json 文件

{
    {
        id: "13",
        id_user: "2",
        login: "ajmal@freedomofdev.com",``
        price_category: "Senior",
        email: "ajmal@freedomofdev.com",
        id_tracker: "16",
        id_owner: "2",
        id_task: "8",
        id_tag: null,
        id_customer: "2",
        id_customer_tracker: "9",
        iscompany: "1",
        color: "048dc4",
        name: "SRA",
        path: "Freedom of dev > Solution reporting > SRA",
        path_relative_to_customer: "Solution reporting > SRA",
        type: "2",
        date_start: "2020-03-06 08:00:00",
        date_end: "2020-03-06 12:00:00",
        hstart: "08:00",
        hend: "12:00",
        date_start_formatted: "06/03/2020",
        date_end_formatted: "06/03/2020",
        ts_start: "1583478000",
        ts_end: "1583492400",
        tz_offset_start: "3600",
        tz_offset_end: "3600",
        locked: false,
        id_invoice: null,
        invoice_ref: null,
        partial: false,
        comment: "[US Technique] mise en place du projet",
        quantity_not_rounded: 4,
        quantity: 4,
        quantity_set: false,
        default_quantity: 4,
        seconds: 14400,
        duration: "4h00",
        hours: 4,
        hascomment: null,
        eventuri: null,
        eventuid: null,
        copy_quantity_from: null,
        location: null,
        item_count: 0,
        invoice_status: "none",
        planning_status: "done",
        future: false,
        time_billing: "normal",
        invoiceable: "1",
        budget_source: null,
        budget_invoice: null,
        budget_periodicity: null,
        unitary_price: "7.000000",
        unitary_cost: "0.000000",
        id_price_level: null,
        path_relative_to_customer_item_time: null,
        label: "Solution reporting > SRA",
        description: "",
        id_unit: "1000",
        unit: "h",
        unit_hours: "1.00",
        base_unitary_price: "7.00000000",
        id_vat_type: null,
        ignored: "0",
        apply_zones: "0",
        price: 28,
        priceByZone:
        {
            normal: 7,
            overtime: 7,
            weekend: 7
        },
        secondsByZone:
        {
            normal: 14400,
            overtime: 0,
            weekend: 0,
            total: 14400,
            main: "normal"
        }
    }
}

类文件如下:

namespace Reporting.Models.TimeTrack
{
    public class PriceByZone
    {
        public int normal { get; set; }
        public int overtime { get; set; }
        public int weekend { get; set; }
    }

    public class SecondsByZone
    {
        public int normal { get; set; }
        public int overtime { get; set; }
        public int weekend { get; set; }
        public int total { get; set; }
        public string main { get; set; }
    }

    public class RootObjectTT
    {
        public string id { get; set; }
        public string id_user { get; set; }
        public string login { get; set; }
        public string price_category { get; set; }
        public string email { get; set; }
        public string id_tracker { get; set; }
        public string id_owner { get; set; }
        public string id_task { get; set; }
        public object id_tag { get; set; }
        public string id_customer { get; set; }
        public string id_customer_tracker { get; set; }
        public string iscompany { get; set; }
        public string color { get; set; }
        public string name { get; set; }
        public string path { get; set; }
        public string path_relative_to_customer { get; set; }
        public string type { get; set; }
        public string date_start { get; set; }
        public string date_end { get; set; }
        public string hstart { get; set; }
        public string hend { get; set; }
        public string date_start_formatted { get; set; }
        public string date_end_formatted { get; set; }
        public string ts_start { get; set; }
        public string ts_end { get; set; }
        public string tz_offset_start { get; set; }
        public string tz_offset_end { get; set; }
        public bool locked { get; set; }
        public object id_invoice { get; set; }
        public object invoice_ref { get; set; }
        public bool partial { get; set; }
        public string comment { get; set; }
        public int quantity_not_rounded { get; set; }
        public int quantity { get; set; }
        public bool quantity_set { get; set; }
        public int default_quantity { get; set; }
        public int seconds { get; set; }
        public string duration { get; set; }
        public int hours { get; set; }
        public object hascomment { get; set; }
        public object eventuri { get; set; }
        public object eventuid { get; set; }
        public object copy_quantity_from { get; set; }
        public object location { get; set; }
        public int item_count { get; set; }
        public string invoice_status { get; set; }
        public string planning_status { get; set; }
        public bool future { get; set; }
        public string time_billing { get; set; }
        public string invoiceable { get; set; }
        public object budget_source { get; set; }
        public object budget_invoice { get; set; }
        public object budget_periodicity { get; set; }
        public string unitary_price { get; set; }
        public string unitary_cost { get; set; }
        public object id_price_level { get; set; }
        public object path_relative_to_customer_item_time { get; set; }
        public string label { get; set; }
        public string description { get; set; }
        public string id_unit { get; set; }
        public string unit { get; set; }
        public string unit_hours { get; set; }
        public string base_unitary_price { get; set; }
        public object id_vat_type { get; set; }
        public string ignored { get; set; }
        public string apply_zones { get; set; }
        public int price { get; set; }
        public PriceByZone priceByZone { get; set; }
        public SecondsByZone secondsByZone { get; set; }
    }
}

但我仍然遇到反序列化问题

我找不到错在哪里

【问题讨论】:

  • JSON 无效。您在对象中有一个对象,但内部对象不是父对象的属性。此外,您还有这一行: login: "ajmal@freedomofdev.com",`` - `` 不应该在那里。我建议就他们返回的无效 JSON 联系 API 维护人员。

标签: c# json rest api


【解决方案1】:

您的 json 不是有效的 json。首先你有这行: login: "ajmal@freedomofdev.com",- the 不应该在那里。

其次,下面的属性示例中应该有双引号

"id": "13", “id_user”:“2”,

尝试在 https://jsonformatter.org/ 验证您的 json

【讨论】:

  • 但是解决办法是什么? :-)
【解决方案2】:

首先,您的 json 文件不是有效的 json,而不是使用 { { ... } },您应该使用 { ... }[{ ... }] 作为对象数组

其次,你的属性字段应该被引用,例如"id":5

这是你的有效 json

{
"id": "13",
"id_user": "2",
"login": "ajmal@freedomofdev.com",
"price_category": "Senior",
"email": "ajmal@freedomofdev.com",
"id_tracker": "16",
"id_owner": "2",
"id_task": "8",
"id_tag": null,
"id_customer": "2",
"id_customer_tracker": "9",
"iscompany": "1",
"color": "048dc4",
"name": "SRA",
"path": "Freedom of dev > Solution reporting > SRA",
"path_relative_to_customer": "Solution reporting > SRA",
"type": "2",
"date_start": "2020-03-06 08:00:00",
"date_end": "2020-03-06 12:00:00",
"hstart": "08:00",
"hend": "12:00",
"date_start_formatted": "06/03/2020",
"date_end_formatted": "06/03/2020",
"ts_start": "1583478000",
"ts_end": "1583492400",
"tz_offset_start": "3600",
"tz_offset_end": "3600",
"locked": false,
"id_invoice": null,
"invoice_ref": null,
"partial": false,
"comment": "[US Technique] mise en place du projet",
"quantity_not_rounded": 4,
"quantity": 4,
"quantity_set": false,
"default_quantity": 4,
"seconds": 14400,
"duration": "4h00",
"hours": 4,
"hascomment": null,
"eventuri": null,
"eventuid": null,
"copy_quantity_from": null,
"location": null,
"item_count": 0,
"invoice_status": "none",
"planning_status": "done",
"future": false,
"time_billing": "normal",
"invoiceable": "1",
"budget_source": null,
"budget_invoice": null,
"budget_periodicity": null,
"unitary_price": "7.000000",
"unitary_cost": "0.000000",
"id_price_level": null,
"path_relative_to_customer_item_time": null,
"label": "Solution reporting > SRA",
"description": "",
"id_unit": "1000",
"unit": "h",
"unit_hours": "1.00",
"base_unitary_price": "7.00000000",
"id_vat_type": null,
"ignored": "0",
"apply_zones": "0",
"price": 28,
"priceByZone": {
  "normal": 7,
  "overtime": 7,
  "weekend": 7
},
"secondsByZone": {
  "normal": 14400,
  "overtime": 0,
  "weekend": 0,
  "total": 14400,
  "main": "normal"
}
}

将其放入对象的代码很简单

var rootObjectTT = JsonConvert.DeserializeObject<RootObjectTT>(jsonexample);

    Console.WriteLine(rootObjectTT.priceByZone.normal);
    Console.WriteLine(rootObjectTT.secondsByZone.normal);
    Console.WriteLine(rootObjectTT.price);

希望有帮助

【讨论】:

  • json 是我无法更改的 api 的响应
  • 提供json的api是否触手可及?像其他团队或其他部门?我认为你应该警告他们他们返回错误格式的 json
猜你喜欢
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
相关资源
最近更新 更多