Newtonsoft JSON - 在JArray中创建JArray

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Newtonsoft JSON - 在JArray中创建JArray相关的知识,希望对你有一定的参考价值。

我正在尝试使用Newtonsoft JSON API创建JSON数组,但它给了我错误。我想实现像这样的结构

[

    {
        "id":"26",
        "appsurvey":"1",
        "fk_curriculumid":"70",
        "status":"Completed",
        "lastaccessedon":"2014-06-20 09:18:54",
        "questions":[
            {
                "feedback":"6",
                "questionid":"1"
            },
            {
                "feedback":"8",
                "questionid":"2"
            },
            {
                "feedback":"1",
                "questionid":"3"
            }
        ],
        "fk_clientid":"24",
        "learnerid":"260"
    }

]

我想多次添加问题数组,但它给了我错误qazxsw poi

这是我的代码:

Can not add property questions to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.

任何人都可以纠正我。提前致谢。

更新:surveytrackD有json数据,

JArray surveytrackingA = new JArray();
/*code to add
[

    {"id":"26",
        "appsurvey":"1",
        "fk_curriculumid":"70",
        "status":"Completed",
        "lastaccessedon":"2014-06-20 09:18:54"}]
*/
for (int i = 0; i < surveytrackingA.Count; i++)
{

    JObject surveytrackD = (JObject)surveytrackingA[i];
    string queryOne = "select * from table101 where fk_curriculumid='"
            + surveytrackD["fk_curriculumid"].ToString()
            + "' and fk_surveyid='"
            + surveytrackD["appsurvey"].ToString() + "'";

    JArray questionsA = new JArray();

    using (var stmt = await App.localDB.PrepareStatementAsync(queryOne))
    {
        while (await stmt.StepAsync())
        {
            JObject questionD = new JObject();
            questionD.Add("questionid", stmt.GetTextAt(5));
            questionD.Add("feedback", stmt.GetTextAt(6));
            questionsA.Add(questionD);
        }
    }                    
    surveytrackD.Add("questions", questionsA); /*error occurred here when second question array is getting inserted in surveyTrackD*/
    surveytrackingA.Add(surveytrackD);
}
答案

您可以使用常规C#类实现相同的结果(JArray中的JArray),最后序列化为JSon。

我在{ "fk_clientid": "24", "learnerid": "260", "appsurvey": "1", "id": "26", "fk_curriculumid": "70", "status": "completed", "lastaccessedon": "2014-06-20 09:18:54" } 发布了一个样本;这里是产生预期输出的代码片段:

Github

输出是:

        var Surveys = new List<SurveytrackD>();

        Surveys.Add( new SurveytrackD { id = "26", appsurvey = "1", fk_curriculumid = "70", status = "Completed", learnerid = "240"  } );
        Surveys.Add( new SurveytrackD { id = "27", appsurvey = "1", fk_curriculumid = "71", status = "Completed", learnerid = "241" });


        foreach (var survey in Surveys)
        {
            survey.questions = new List<Question>();

            survey.questions.Add(new Question { questionid = "1", feedback = "0" });
            survey.questions.Add(new Question { questionid = "2", feedback = "1" });
        }

        var json = JsonConvert.SerializeObject(Surveys, Formatting.Indented);

        Console.WriteLine(json);

以上是关于Newtonsoft JSON - 在JArray中创建JArray的主要内容,如果未能解决你的问题,请参考以下文章

无法将“Newtonsoft.Json.Linq.JObject”类型的对象强制转换为“Newtonsoft.Json.Linq.JArray”

如何”Newtonsoft.Json.Linq.JValue”的对象强制转换为类型“Newtonsoft.Json.Linq.JArray"?

无法将“Newtonsoft.Json.Linq.JArray”类型的对象转换为“System.Collections.Generic.List”类型

c# 读取json的问题,JObject不能强转成JArray

JObject 用法 JProperty 用法JArray 用法 Linq 转 Json

C# dynamic类型序列化和反序列化之Newtonsoft.Json,动态解析远端返回的jSON数据