如何反序列化JSON数组(平面)并忽略某些标记

时间:2017-08-30 11:34:27

标签: c# json deserialization json-deserialization

我有来自服务器的响应

 [{
  "sys_id": "******************************",
  "dv_model_id": "*****************",
  "due": "YYYY-MM-DD HH:mm:ss",
  "assigned_to": "1524s32a54dss412s121s",
  "dv_assigned_to": "username",
  "assigned_to.phone": "+12345678910",
  "assigned_to.email": "abc@a.c",
  "u_borrower_id": "fb36e45f0a12452004742183457e833b0",
  "dv_u_borrower_id": "antoherUserName",
  "u_borrower_id.phone": "+12345678910",
  "u_borrower_id.email": "abcf@a.c"
}
,{....}
,{....}]

我正在尝试将其反序列化为List

public class Inventory
{
    public Inventory()
    {
        assigned_to = new User();
        u_borrower_wwid = new User();
    }

    public string sys_ID { get; set; }
    public string dv_model_id { get; set; }
    public DateTime due { get; set; }
    public string dv_assigned_to { get; set; }
    public User assigned_to { get; set; }
    public string dv_u_borrower_id { get; set; }
    public User u_borrower_id { get; set; }
}

现在,因为JSON包含 - “assigned_to”:“1524s32a54dss412s121s”,“ 反序列化失败了。 与 - “”u_borrower_id“相同:”fb36e45f0a12452004742183457e833b0“,”。

你知道怎么忽略它们吗?或者从JSON中删除它们? 我只需要对象的属性(“.phone”和“.email”)。

任何想法?

2 个答案:

答案 0 :(得分:2)

我看到了许多解决方案:

修改您的Inventory对象(或创建一个新对象),以便可以完全反序列化json,然后访问那里的值。

您的新对象应如下所示:

public class InventoryJsonObject
{
    public string sys_id { get; set; }
    public string dv_model_id { get; set; }
    public string due { get; set; }
    public string assigned_to { get; set; }
    public string dv_assigned_to { get; set; }

    [JsonProperty("assigned_to.phone")]
    public string assigned_to_phone { get; set; }

    [JsonProperty("assigned_to.email")]
    public string assigned_to_email { get; set; }
    public string u_borrower_id { get; set; }
    public string dv_u_borrower_id { get; set; }

    [JsonProperty("u_borrower_id.phone")]
    public string u_borrower_id_phone { get; set; }

    [JsonProperty("u_borrower_id.email")]
    public string u_borrower_id_email { get; set; }
}

使用正则表达式从字符串中获取值。在这种情况下,您的正则表达式将是"u_borrower_id\.phone": "(.*?)""u_borrower_id\.email": "(.*?)"

完整的正则表达式解决方案可能如下所示(假设每个对象都包含电话和电子邮件):

string phonePattern = "\"u_borrower_id\\.phone\": \"(.*?)\"";
string emailPattern = "\"u_borrower_id\\.email\": \"(.*?)\"";

Regex phoneRegex = new Regex(phonePattern);
var phoneMatches = phoneRegex.Matches(input);

Regex emailRegex = new Regex(emailPattern);
var emailMatches = emailRegex.Matches(input);

for (int i = 0; i < phoneMatches.Count; i++)
{
    string phoneMatch = phoneMatches[i].Groups[1].Value;
    string emailMatch = emailMatches[i].Groups[1].Value;
    // Now you can add them to any collection you desire
}

stringUser之间实施广告。由于您的错误源于字符fb36e45f0a12452004742183457e833b0无法投放到User这一事实对象很简单,你必须实现强制转换。它看起来像这样:

public static implicit operator User(string _string)
{
    // This could be a DB lookup, or basically anything else
    return new User()
    {
        id = _string
    };
}

答案 1 :(得分:0)

为了避免导致错误的不必要的强制转换,您可以通过创建Dictionary<string, object>来使用此变通方法  并且只读取您需要的属性作为字符串并将其转换为您想要的类型:

using System.Web.Script;

Dictionary<string, object> dict = Serialization.JavaScriptSerializer().Deserialize<Dictionary<string, object>>(json);

现在您可以逐个修改类属性,如: (您可以为DateTime和User属性创建其他方法)

Inventory inventory = new Inventory();

//notice i'v added the 'u_borrower_id_email' property to your class:
inventory.u_borrower_id_email = dict.GetStringOrDefault("u_borrower_id.phone");

private static string GetStringOrDefault(this Dictionary<string, object> data, string key)
{
    string result = "";
    object o;
    if (data.TryGetValue(key, out o))
    {
        if (o != null)
        {
            result = o.ToString();
        }
    }
    return result;
}