如何反序列化 JSON array(flat) 并忽略一些标记

How do I deserialize a JSON array(flat) and ignore some token

我收到来自服务器的响应

 [{
  "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"
}
,{....}
,{....}]

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

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”)。

有什么想法吗?

我看到了一些解决方案:


修改您的 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": "(.*?)"

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

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

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

using System.Web.Script;

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

现在您可以像这样一一修改您的 class 属性: (您可以为日期时间和用户创建其他方法 属性)

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