JSON 的反序列化包含 "Unique Values" - RestSharp

Deserialization of JSON containing "Unique Values" - RestSharp

我看过反序列化 JSON 的示例,但我的 JSON 具有唯一值

{
  "-JxsJFiGBqQz1KQmmR0i" : {
    "bizcardData" : {
      "company" : "Tesla",
      "designation" : "Developer",
      "email" : "phani@tesla.com",
      "name" : "Phani",
      "phone" : "5135921241"
    },
    "transData" : {
      "date" : "15-08-29",
      "location" : "39.1395996,-84.5295417",
      "tag" : "sender",
      "time" : "03:17:00"
    }
  },
  "-JxsJKnJIVTFQWE1aSOr" : {
    "bizcardData" : {
      "company" : "Spotify",
      "designation" : "Designer",
      "email" : "komarapa@spotify.com",
      "name" : "Phani Komaravolu",
      "phone" : "5135921241"
    },
    "transData" : {
      "date" : "15-08-29",
      "location" : "39.1395996,-84.5295417",
      "tag" : "sender",
      "time" : "03:17:21"
    }
  }
}

我正在使用一个名为 Transactions 的 Transactions,其中包含 bizCardDatatransData 的 List>。我不需要存储唯一值。只是两个值列表。

请分享如何使用 JSON 反序列化具有唯一值的 JSON 的代码,如果可能,请阐明如何最好地构建 class 以存储值。

谢谢!

RestSharp 支持将具有任意名称的 JSON 对象反序列化为 .Net 字典。给定 类:

public class BizcardData
{
    public string company { get; set; }
    public string designation { get; set; }
    public string email { get; set; }
    public string name { get; set; }
    public string phone { get; set; }
}

public class TransData
{
    public string date { get; set; }
    public string location { get; set; }
    public string tag { get; set; }
    public string time { get; set; }
}

public class Transactions
{
    public BizcardData bizcardData { get; set; }
    public TransData transData { get; set; }
}

您可以像这样反序列化和序列化您的 JSON:

        var resultList = SimpleJson.DeserializeObject<Dictionary<string, Transactions>>(json);

或者,

        RestSharp.RestResponse response = new RestSharp.RestResponse();
        response.Content = json;

        JsonDeserializer serializer = new JsonDeserializer();

        var resultList = serializer.Deserialize<Dictionary<string, Transactions>>(response);