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

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

有什么想法吗?

最佳答案

我看到了很多解决方案:


修改您的 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
    };
}

关于c# - 如何反序列化 JSON 数组(平面)并忽略某些标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45959335/

相关文章:

c# - 将 json 映射到 C# 对象

json - 如何加载 JSON 文件并将其转换为特定类型的对象?

c# - 从文件夹中获取所有 .json 文件,然后使用 C# 和 JSON.Net 序列化为单个 .Json 文件并序列化

c# - 限制相机旋转角度

c# - 将类型库导入为 C# 代码的工具

c# - 每个配置文件只允许一个 configSections 元素,如果存在,则必须是根配置元素的第一个子元素

ios - 在 UITableView 中返回 YouTube channel 的视频列表

javascript - 从 JSON 数据输出元素的递归函数

java - 如何从 json 对象中获取字符串列表

c# - 多态性 WCF 反序列化不起作用