Newtonsoft JSON Десериализация проблемы - PullRequest
0 голосов
/ 03 апреля 2020

у меня был следующий json файл, когда я делаю api-вызов rest

{
    {
        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"
        }
    }
}

, а файл Class следующий:

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; }
    }
}

, но я все еще получаю десериализацию проблема

я не могу найти, где вина

Ответы [ 2 ]

0 голосов
/ 03 апреля 2020

Во-первых, ваш 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);

Надеюсь, что это поможет

0 голосов
/ 03 апреля 2020

Ваш json недействителен json. Прежде всего, у вас есть эта строка: логин: "ajmal@freedomofdev.com", - the не должно быть там.

секунда, в приведенном ниже примере свойства должна быть двойная кавычка

"id" : "13", "id_user": "2",

Попробуйте проверить json на https://jsonformatter.org/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...