json 分析值c# newtonsoft时遇到意外字符

jvidinwx  于 2023-03-24  发布在  C#
关注(0)|答案(1)|浏览(213)

我使用C#与Newtonsoft库,我得到的错误是这样的
分析值时遇到意外字符:{. Path 'data',line 1,position 9.
这是我从call得到的结果json
var result = client.GetAsync(endpoint).Result.Content.ReadAsStringAsync().Result;

{
"data": {
  "0": {
    "code": "1000",
    "name": "Jason",
    "currency": "EUR",
    "best_price": 100,
    "paid": true,
    "products": [
      {
        "name": "Music",
        "price": "free",
        "id": 12345
      }
    ],
    "archive_time": "08:00"
  },
  "1": {
    "code": "2000",
    "name": "James",
    "currency": "EUR",
    "best_price": 100,
    "paid": true,
    "products": [
      {
        "name": "Music",
        "price": "free",
        "id": 98521
      }
    ],
    "archive_time": null
  },
  "paginator": {
    "total_count": 3,
    "total_pages": 1,
    "current_page": 1,
    "per_page": 50,
    "next_page_url": null,
    "prev_page_url": null
  },
  "message": {
    "message": "Success",
    "status_code": 200
  }
}}

这些是我的课

public class test2
{
    public string data { get; set; }
    public string code { get; set; }
    public string name { get; set; }
    public string currency { get; set; }
    public double best_price { get; set; }
    public string paid { get; set; }
    public string archive_time { get; set; }
    public _products products { get; set; }
    public _paginator paginator { get; set; }
    public _message message { get; set; }
}

public class _products
{
    public string name { get; set; }
    public string price { get; set; }
    public int id { get; set; }
}
public class _paginator
{
    public int total_count { get; set; }
    public int total_pages { get; set; }
    public int current_page { get; set; }
    public int per_page { get; set; }
    public bool next_page_url { get; set; }
    public bool prev_page_url { get; set; }
}
public class _message
{
    public string message { get; set; }
    public int status_code { get; set; }
}

我的反序列化代码:

var account = JsonConvert.DeserializeObject<test2>(result.ToString());
7bsow1i6

7bsow1i61#

我认为字典将有利于您的情况下,因为我怀疑你可以有更多的产品不仅仅是“和”1

Data data = JsonConvert.DeserializeObject<Root>(json)?.data;
    Product product0 = data.Products["0"];

public class Root
{
    public Data data { get; set; }
}

public class Data
{
    [JsonExtensionData]
    private IDictionary<string, JToken> _products { get; set; }

    [JsonIgnore]
    public Dictionary<string, Product> Products {get;set;}
    
    public Paginator paginator { get; set; }
    public Message message { get; set; }

    [OnDeserialized]
    private void OnDeserialized(StreamingContext context)
    {
        Products = _products.ToDictionary(v => v.Key, v => v.Value.ToObject<Product>());
    }
}

public class Product
{
    public string code { get; set; }
    public string name { get; set; }
    public string currency { get; set; }
    public int best_price { get; set; }
    public bool paid { get; set; }
    public List<ProductItem> products { get; set; }
    public string archive_time { get; set; }
}

public class Message
{
    public string message { get; set; }
    public int status_code { get; set; }
}

public class Paginator
{
    public int total_count { get; set; }
    public int total_pages { get; set; }
    public int current_page { get; set; }
    public int per_page { get; set; }
    public object next_page_url { get; set; }
    public object prev_page_url { get; set; }
}

public class ProductItem
{
    public string name { get; set; }
    public string price { get; set; }
    public int id { get; set; }
}

相关问题