简体   繁体   English

无法将json反序列化为Dictionary <string, List<Purchase> &gt;

[英]Can not deserialize json to Dictionary<string, List<Purchase>>

I can not deserialize json to Dictionary<string, List<Purchas>> in C#. 我无法在C#中将json反序列化为Dictionary<string, List<Purchas>>

Here is my .json : 这是我的.json

{
  "Ukraine": {
    {
      "Credits": 500,
      "Name": "Clever goat",
      "Price": {
        "Amount": 100,
        "Currency": "UAH"
      }
    },
    {
      "Credits": 1000,
      "Name": "Smart hare",
      "Price": {
        "Amount": 190,
        "Currency": "UAH"
      }
    }
  },

  "USA": {
    {
      "Credits": 500,
      "Name": "Clever goat",
      "Price": {
        "Amount": 10,
        "Currency": "USD"
      }
    },
    {
      "Credits": 1000,
      "Name": "Smart hare",
      "Price": {
        "Amount": 19,
        "Currency": "USD"
      }
    }
  }
}

Here is my Purchase class: 这是我的Purchase课程:

public class Price
{
    public int Amount { get; set; }
    public string Currency { get; set; }
}

public class Purchase
{
    public int Credits { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
}

Here is how I am trying to deserialize it: 这是我试图反序列化的方法:

var countryToPurchases = JsonConvert.DeserializeObject<Dictionary<string, List<Purchase>>>(dataJSON);

Here is the error I am getting: 这是我得到的错误:

JsonReaderException: Invalid property identifier character: {. JsonReaderException:无效的属性标识符字符:{。 Path 'Ukraine', line 3, position 4. Newtonsoft.Json.JsonTextReader.ParseProperty() 路径“乌克兰”,第3行,位置4。Newtonsoft.Json.JsonTextReader.ParseProperty()

What am I missing here? 我在这里想念什么?

You have two problems here: 您在这里有两个问题:

  1. The Json is invalid. Json无效。 There should be arrays there but there aren't. 应该有数组,但没有。
    A valid json would look like this: 有效的json如下所示:
[
  {
    "Ukraine": [
      {
        "Credits": 500,
        "Name": "Clever goat",
        "Price": {
          "Amount": 100,
          "Currency": "UAH"
        }
      },
      {
        "Credits": 1000,
        "Name": "Smart hare",
        "Price": {
          "Amount": 190,
          "Currency": "UAH"
        }
      }
    ]
  },
  {
    "USA": [
      {
        "Credits": 500,
        "Name": "Clever goat",
        "Price": {
          "Amount": 10,
          "Currency": "USD"
        }
      },
      {
        "Credits": 1000,
        "Name": "Smart hare",
        "Price": {
          "Amount": 19,
          "Currency": "USD"
        }
      }
    ]
  }
]
  1. The Price property should be of type Price , not int . Price属性应为Price类型,而不是int类型。
public class Purchase
{
    public int Credits { get; set; }
    public string Name { get; set; }
    public Price Price { get; set; }
}

please check this json and see if it's works : 请检查此json,看看是否可行:

 {
 "Ukraine": {
  "Credits": 500,
  "Name": "Clever goat",
  "Price": {
   "Amount": 100,
   "Currency": "UAH"
 }
},
 "USA": {
  "Credits": 500,
  "Name": "Clever goat",
  "Price": {
    "Amount": 10,
    "Currency": "USD"
  }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何将Json反序列化为Dictionary <String,List<String> &gt; - How I can deserialize Json into Dictionary<String,List<String>> 将 JSON 反序列化为字典<string, List<string> &gt; - Deserialize JSON to Dictionary<string, List<string>> 我如何反序列化json字符串列表 <Dictionary<string,int> &gt;使用JQuery? - How can I deserialize a json string List<Dictionary<string,int>> using JQuery? 试图将 JSON 反序列化为字典<string, list<string> &gt; 在 c#</string,> - Trying to deserialize JSON into Dictionary<string, List<string>> in c# 无法将JSON反序列化为字典 <string, List<string> &gt;使用JavaScriptSerializer - Unable to deserialize JSON to a Dictionary<string, List<string>> using JavaScriptSerializer 如何反序列化包含列表和字典(或键值)属性对象的json字符串 - How can I deserialize a json string contianing a list and dictionary (or keyvalue) property objects 如何将JSON文档反序列化为Dictionary <string, List<object> &gt; - How do I deserialize a JSON document into a Dictionary<string, List<object>> 将JSON反序列化为Dictionary <string,Object> - Deserialize JSON into Dictionary<string,Object> 无法反序列化json列表 - can not deserialize json list 将 JSON 字符串反序列化为字典<string,object> - Deserialize JSON string to Dictionary<string,object>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM