相关疑难解决方法(0)

如何使用JSON.net处理同一属性的单个项目和数组

我正在尝试修复我的SendGridPlus库来处理SendGrid事件,但是我在API中对类别的处理方式不一致时遇到了一些麻烦.

在以下示例从SendGrid API引用获取的有效内容中,您会注意到category每个项的属性可以是单个字符串,也可以是字符串数组.

[
  {
    "email": "john.doe@sendgrid.com",
    "timestamp": 1337966815,
    "category": [
      "newuser",
      "transactional"
    ],
    "event": "open"
  },
  {
    "email": "jane.doe@sendgrid.com",
    "timestamp": 1337966815,
    "category": "olduser",
    "event": "open"
  }
]
Run Code Online (Sandbox Code Playgroud)

看起来像我这样制作JSON.NET的选择是在字符串进入之前修复字符串,或者配置JSON.NET以接受不正确的数据.如果我能逃脱它,我宁愿不做任何字符串解析.

有没有其他方法我可以使用Json.Net处理这个?

json.net sendgrid

82
推荐指数
3
解决办法
3万
查看次数

如何处理返回字符串和字符串数组的json?

我正在使用Yahoo fantasy sports api.我得到这样的结果:

"player": [
    {
        ...
        "eligible_positions": {
            "position": "QB"
        },
        ...
    },
    {
        ...
        "eligible_positions": {
            "position": [
                "WR",
                "W/R/T"
            ]
        },
        ...
    },
Run Code Online (Sandbox Code Playgroud)

我怎么能反序化呢?

我的代码看起来像这样:

var json = new JavaScriptSerializer();

if (response != null)
{
    JSONResponse JSONResponseObject = json.Deserialize<JSONResponse>(response);
    return JSONResponseObject;
}
Run Code Online (Sandbox Code Playgroud)

在我的JSONResponse.cs文件中:

public class Player
{
    public string player_key { get; set; }
    public string player_id { get; set; }
    public string display_position { get; set; }        
    public SelectedPosition selected_position { get; set; }
    public …
Run Code Online (Sandbox Code Playgroud)

c# json

9
推荐指数
1
解决办法
1604
查看次数

标签 统计

c# ×1

json ×1

json.net ×1

sendgrid ×1