c# - Newtonsoft JSON - create JArray in JArray -
c# - Newtonsoft JSON - create JArray in JArray -
i trying create json array using newtonsoft json api giving me error. want accomplish construction
[ { "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" } ]
i want ot add together questions array multiple time giving me error can not add together property questions newtonsoft.json.linq.jobject. property same name exists on object.
here code:
jarray surveytrackinga = new jarray(); /*code add together [ {"id":"26", "appsurvey":"1", "fk_curriculumid":"70", "status":"completed", "lastaccessedon":"2014-06-20 09:18:54"}] */ (int = 0; < surveytrackinga.count; i++) { jobject surveytrackd = (jobject)surveytrackinga[i]; string queryone = "select * table101 fk_curriculumid='" + surveytrackd["fk_curriculumid"].tostring() + "' , 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 sec question array getting inserted in surveytrackd*/ surveytrackinga.add(surveytrackd); }
can please right me. in advance.
update: surveytrackd have json data,
{ "fk_clientid": "24", "learnerid": "260", "appsurvey": "1", "id": "26", "fk_curriculumid": "70", "status": "completed", "lastaccessedon": "2014-06-20 09:18:54" }
you can accomplish same result (jarray in jarray) using regular c# classes , @ end serialize json.
i posted sample in github; here fragment of code produces expected output:
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);
the output is:
[ { "fk_clientid": null, "learnerid": "240", "appsurvey": "1", "id": "26", "fk_curriculumid": "70", "status": "completed", "lastaccessedon": null, "questions": [ { "feedback": "0", "questionid": "1" }, { "feedback": "1", "questionid": "2" } ] }, { "fk_clientid": null, "learnerid": "241", "appsurvey": "1", "id": "27", "fk_curriculumid": "71", "status": "completed", "lastaccessedon": null, "questions": [ { "feedback": "0", "questionid": "1" }, { "feedback": "1", "questionid": "2" } ] } ]
c# json windows-phone-8 json.net
Comments
Post a Comment