小编Kla*_*aus的帖子

使用LINQ创建JSON的异常

我正在尝试使用以下代码创建JSON:

        JArray jInner = new JArray("document");
        JProperty jTitle = new JProperty("title", category);
        JProperty jDescription = new JProperty("description", "this is the description");
        JProperty jContent = new JProperty("content", content);
        jInner.Add(jTitle);
        jInner.Add(jDescription);
        jInner.Add(jContent);
Run Code Online (Sandbox Code Playgroud)

当我到达时jInner.Add(jTitle),我得到以下异常:

System.ArgumentException: Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray.
   at Newtonsoft.Json.Linq.JContainer.ValidateToken(JToken o, JToken existing)
   at Newtonsoft.Json.Linq.JContainer.InsertItem(Int32 index, JToken item, Boolean skipParentCheck)
   at Newtonsoft.Json.Linq.JContainer.AddInternal(Int32 index, Object content, Boolean skipParentCheck)
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮忙告诉我,我做错了什么?

c# linq json json.net

5
推荐指数
1
解决办法
7101
查看次数

标签 统计

c# ×1

json ×1

json.net ×1

linq ×1