包含"唯一值的JSON的反序列化" - RestSharp

时间:2015-08-30 02:08:24

标签: c# json deserialization 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"
    }
  }
}

我正在使用一个包含List>的名为Transactions的交易对于 bizCardData transData 。我不需要存储唯一值。只是两个值列表。

请使用JSON分享如何使用唯一值反序列化JSON的代码,如果可能的话,请详细说明如何构建最佳类,以存储值。

谢谢!

1 个答案:

答案 0 :(得分:1)

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