代码之家  ›  专栏  ›  技术社区  ›  Sunitha Bist

如果找到数组而不是对象usinc C,如何对json字符串进行异常处理#

  •  1
  • Sunitha Bist  · 技术社区  · 7 年前

    [
        { 
            "productId":"17213812",
            "returnPolicies": {
                "user":"Regular Guest"
            }
        },
        {
            "productId":"17813832",
            "returnPolicies":[]
        }
    ] 
    

    我得到了一个错误 returnPolicies returnpoliciesuser 所以如果我使用 e.returnpolicies.user 它显示第二个数组的错误。。 "returnPolicies":[] 和仅搜索 e.returnpolicies.user("returnPolicies":{"user":"Regular Guest"}) ?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Cinchoo    7 年前

    这就是你可以做到的。

    using (var jr = new ChoJSONReader("sample6.json")
        .WithField("ProductId", jsonPath: "$.productId")
        .WithField("User", jsonPath: "$.returnPolicies.user")
        )
    {
        foreach (var item in jr)
            Console.WriteLine(item.ProductId + " " + item.User);
    }
    
    推荐文章