Sunday, 15 August 2010

How to read JSON Response for elements with no name in Java (SurveyGizmo survey response API) -



How to read JSON Response for elements with no name in Java (SurveyGizmo survey response API) -

i'm working surveygizmo api retrieve survey responses, in json response (example below) came across 2 issues.

how iterate through question:answer pairs, there doesn't seem field name associated can't jsonobject.getjsonobject("fieldname"); or jsonarray.getjsonarray("fieldname"); due skip logic of survey won't know questionid's need pull in can't 'question(#)' either.

i need questionid of question, given key "[question(#)]", how obtain questionid since don't believe either jsonobject or jsonarray, should treat string , regex search retrieve # in "question()"?

jsonresponse example

{ "result_ok":true, "total_count":"1", "page":1, "total_pages":1, "results_per_page":50, "data":[{ "id":"1", "contact_id":"", "status":"complete", "is_test_data":"1", "datesubmitted":"2013-02-07 12:00:00", "sresponsecomment":"", "[question(3)]":"15", "[question(4), option(10003)]":"baseball", "[question(4), option(10007)]":"basketball", "[question(4), option(10009)]":"hockey", "[question(12)]":"no", "[question(14)]":"yes", "[question(15)]":"abc", "[question(16)]":"no" }] }

i have been working on similar project. here have far.

model.cs:

using newtonsoft.json; using system; using system.collections.generic; using system.componentmodel.dataannotations; using system.linq; using system.text; using system.text.regularexpressions; using system.threading.tasks; namespace deserializer { [jsonobject] public class responses { public bool result_ok { get; set; } public string total_count { get; set; } public int page { get; set; } public int total_pages { get; set; } public int results_per_page { get; set; } public surveyresponse[] info { get; set; } } [jsonobject] // here magic: when see type, utilize class read it. // if want, can define jsonconverter adding // jsonserializer, , parsing that. [jsonconverter(typeof(dataitemconverter))] public class surveyresponse { public string id { get; set; } public string contact_id { get; set; } public string status { get; set; } public string is_test_data { get; set; } public datetime datesubmitted { get; set; } public string sresponsecomment { get; set; } public list<surveyquestion> surveyquestions { get; set; } public list<surveyurl> surveyurls { get; set; } public list<surveygeodata> surveygeodatas { get; set; } public list<surveyvariable> surveyvariables { get; set; } public list<surveyvariableshown> surveyvariableshowns { get; set; } public list<surveyquestionhidden> surveyquestionhiddens { get; set; } public list<surveyquestionoption> surveyquestionoptions { get; set; } public list<surveyquestionmulti> surveyquestionmulties { get; set; } } public class surveyquestion { [key] public int questionid { get; set; } public string questionresponse { get; set; } } public class surveyurl { [key] public int surveyurlid { get; set; } public string name { get; set; } public string value { get; set; } } public class surveygeodata { [key] public int surveygeodataid { get; set; } public string name { get; set; } public string value { get; set; } } public class surveyvariable { [key] public int surveyvariableid { get; set; } public string value { get; set; } } public class surveyvariableshown { [key] public int surveyvariableshownid { get; set; } public string name { get; set; } public string value { get; set; } } public class surveyquestionhidden { [key] public int questionid { get; set; } public string questionresponse { get; set; } } public class surveyquestionoption { [key] public int optionid { get; set; } public int questionid { get; set; } public string questionresponse { get; set; } } public class surveyquestionmulti { [key] public int optionid { get; set; } public int questionid { get; set; } public string questionresponse { get; set; } } public class dataitemconverter : jsonconverter { public override bool canconvert(type objecttype) { homecoming objecttype == typeof(surveyresponse); } public override bool canread { { homecoming true; } } public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer) { var value = (surveyresponse)existingvalue; if (value == null) { value = new surveyresponse(); value.surveyquestions = new list<surveyquestion>(); value.surveyurls = new list<surveyurl>(); value.surveygeodatas = new list<surveygeodata>(); value.surveyvariables = new list<surveyvariable>(); value.surveyvariableshowns = new list<surveyvariableshown>(); value.surveyquestionhiddens = new list<surveyquestionhidden>(); value.surveyquestionoptions = new list<surveyquestionoption>(); value.surveyquestionmulties = new list<surveyquestionmulti>(); } // skip opening { reader.read(); while (reader.tokentype == jsontoken.propertyname) { var name = reader.value.tostring(); reader.read(); // here magic string input = name; //[question(1)] //[question(11)] //[question(111)] //[question(1234)] //[question(12345)] //[url(12345)] //[variable(12345)] //single reply match matchsingleanswer = regex.match(input, @"\[(question|calc|comment)\(([0-9]{5}|[0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]{1})\)]", regexoptions.ignorecase); //single variable match matchsinglevariable = regex.match(input, @"\[(variable)\(([0-9]{5}|[0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]{1})\)]", regexoptions.ignorecase); //url match matchurl = regex.match(input, @"\[url", regexoptions.ignorecase); //geo info match matchgeo = regex.match(input, @"\[variable\(""standard_", regexoptions.ignorecase); //variables shown match matchvariables = regex.match(input, @"\[variable", regexoptions.ignorecase); //[question(1), option(\"1 //[question(11), option(\"2 //[question(111), option(\"1 //[question(1234), option(\"1 //[question(12345), option(\"1 //////////////////////////////////////////// ////////the \ values beingness removed. //////////////////////////////////////////// //optional answers string myreg = @"\[(question|url|variable|calc|comment)\(([0-9]{5}|[0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]{1})\),\ option\(""[0-9]"; match matchoption = regex.match(input, myreg, regexoptions.ignorecase); //[question(1), option(1 //[question(11), option(2 //[question(111), option(1 //[question(1234), option(1 //[question(12345), option(1 //multiple selection match matchmultiselect = regex.match(input, @"\[question\(([0-9]{5}|[0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]{1})\),\ option\([0-9]", regexoptions.ignorecase); //[question(1), option(0) //[question(11), option(0) //[question(111), option(0) //[question(1234), option(0) //[question(12345), option(0) //hidden match matchhiddenvalue = regex.match(input, @"\[question\(([0-9]{5}|[0-9]{4}|[0-9]{3}|[0-9]{2}|[0-9]{1})\),\ option\(0\)", regexoptions.ignorecase); if (matchsingleanswer.success) { int index = int.parse(name.substring(10, name.indexof(')') - 10)); surveyquestion sq = new surveyquestion(); sq.questionid = index; sq.questionresponse = serializer.deserialize<string>(reader); value.surveyquestions.add(sq); } else if (matchurl.success) { string urlname = name.substring(6, name.length - 9); surveyurl su = new surveyurl(); su.name = urlname; su.value = serializer.deserialize<string>(reader); value.surveyurls.add(su); } else if (matchgeo.success) { string geoname = name.substring(11, name.length - 14); surveygeodata sgd = new surveygeodata(); sgd.name = geoname; sgd.value = serializer.deserialize<string>(reader); value.surveygeodatas.add(sgd); } else if (matchsinglevariable.success) { int index = int.parse(name.substring(10, name.indexof(')') - 10)); surveyvariable sv = new surveyvariable(); sv.surveyvariableid = index; sv.value = serializer.deserialize<string>(reader); value.surveyvariables.add(sv); } else if (matchvariables.success) { string varname = name.substring(11, name.length - 14); surveyvariableshown svs = new surveyvariableshown(); svs.name = varname; svs.value = serializer.deserialize<string>(reader); value.surveyvariableshowns.add(svs); } else if (matchhiddenvalue.success) { int index = int.parse(name.substring(10, name.indexof(')') - 10)); surveyquestionhidden sqh = new surveyquestionhidden(); sqh.questionid = index; sqh.questionresponse = serializer.deserialize<string>(reader); value.surveyquestionhiddens.add(sqh); } else if (matchmultiselect.success) { //multiple selection question selections string[] namearray = name.split(')'); string questionpart = namearray[0]; string optionpart = namearray[1]; int index = int.parse(questionpart.substring(10, questionpart.length - 10)); int indexsub = int.parse(optionpart.substring(9, optionpart.length - 9)); surveyquestionmulti sqm = new surveyquestionmulti(); sqm.optionid = indexsub; sqm.questionid = index; sqm.questionresponse = serializer.deserialize<string>(reader); value.surveyquestionmulties.add(sqm); //need add together base of operations question point multi //surveyquestion sq = new surveyquestion(); //sq.questionid = sqm.questionid; //sq.questionresponse = ""; //value.surveyquestions.add(sq); } else if (matchoption.success) { //optional text value given question string[] namearray = name.split(')'); string questionpart = namearray[0]; string optionpart = namearray[1]; int index = int.parse(questionpart.substring(10, questionpart.length - 10)); int indexsub = int.parse(optionpart.substring(10, 5)); surveyquestionoption sqo = new surveyquestionoption(); sqo.optionid = indexsub; sqo.questionid = index; sqo.questionresponse = serializer.deserialize<string>(reader); value.surveyquestionoptions.add(sqo); } else { var property = typeof(surveyresponse).getproperty(name); if (property != null) property.setvalue(value, serializer.deserialize(reader, property.propertytype), null); } // skip , or } if @ end reader.read(); } homecoming value; } public override bool canwrite { { homecoming false; } } public override void writejson(jsonwriter writer, object value, jsonserializer serializer) { throw new notimplementedexception(); } } }

and, the main function program.cs file:

string webreq = string.empty; //office energy - allan testing webreq += "https://restapi.surveygizmo.com/head/survey/xxxxxx"; webreq += "/surveyresponse/"; webreq += "?user:pass=xxxxxxxx:xxxxxxx"; webreq += "&page=101&resultsperpage=1"; httpwebrequest request = webrequest.create(webreq) httpwebrequest; var results = string.empty; using (httpwebresponse response = request.getresponse() httpwebresponse) { streamreader reader = new streamreader(response.getresponsestream()); results += reader.readtoend(); } responses responses = new javascriptserializer().deserialize<responses>(results); console.writeline("feed headers:"); console.writeline(""); console.writeline("result_ok: " + responses.result_ok); console.writeline("total_count: " + responses.total_count); console.writeline("page: " + responses.page); console.writeline("total_pages: " + responses.total_pages); console.writeline("results_per_page: " + responses.results_per_page); console.writeline(""); console.writeline(""); foreach (var item in responses.data) { console.writeline("id: " + item.id); console.writeline("contact_id: " + item.contact_id); console.writeline("status: " + item.status); console.writeline("is_test_data: " + item.is_test_data); console.writeline("datesubmitted: " + item.datesubmitted); console.writeline("sresponsecomment: " + item.sresponsecomment); console.writeline(""); } console.writeline("press come in continue..."); console.readline(); //using live stream var result = jsonconvert.deserializeobject<responses>(results); //local results (not using live stream) //var result = jsonconvert.deserializeobject<responses>(localresults); //want calcualte highest kay number loop //http://stackoverflow.com/questions/2805703/good-way-to-get-the-key-of-the-highest-value-of-a-dictionary-in-c-sharp //var max = result.data[0].questions[0].aggregate((l, r) => l.key > r.key ? l : r).key; foreach (var item in result.data[0].surveyquestions) { string label = "questionid = " + item.questionid; string val = "questionresponse = " + item.questionresponse; console.writeline(label); console.writeline(val); console.writeline(""); //question alternative var listoptions = result.data[0].surveyquestionoptions; var listlocalquestionoptions = o in listoptions o.questionid == item.questionid select o; foreach (var itemsub in listlocalquestionoptions) { console.writeline(" optional"); string labelsub1 = " optionid = " + itemsub.optionid; string labelsub2 = " questionid = " + itemsub.questionid; string valsub1 = " questionresponse = " + itemsub.questionresponse; console.writeline(labelsub1); console.writeline(labelsub2); console.writeline(valsub1); console.writeline(""); } //question multi var listmulties = result.data[0].surveyquestionmulties; var listlocalquestionmulties = m in listmulties m.questionid == item.questionid select m; foreach (var itemsub in listlocalquestionmulties) { console.writeline("multies"); string labelsub1 = " optionid = " + itemsub.optionid; string labelsub2 = " questionid = " + itemsub.questionid; string valsub1 = " questionresponse = " + itemsub.questionresponse; console.writeline(labelsub1); console.writeline(labelsub2); console.writeline(valsub1); console.writeline(""); } } //console.writeline(""); //console.writeline(""); //console.writeline(""); //console.writeline(""); //foreach (var item in result.data[0].surveyquestionoptions) //{ // string label = "optionid = " + item.optionid; // string label2 = "questionid = " + item.questionid; // string val = "questionresponse = " + item.questionresponse; // console.writeline(label); // console.writeline(label2); // console.writeline(val); // console.writeline(""); //} //console.writeline(""); console.writeline("press come in continue..."); console.readline(); console.writeline(""); console.writeline(""); console.writeline("hiddens"); console.writeline(""); foreach (var item in result.data[0].surveyquestionhiddens) { string label = "questionid = " + item.questionid; string val = "questionresponse = " + item.questionresponse; console.writeline(label); console.writeline(val); console.writeline(""); } console.writeline("press come in continue..."); console.readline(); console.writeline(""); console.writeline(""); console.writeline("multies"); console.writeline(""); foreach (var item in result.data[0].surveyquestionmulties) { string label = "optionid = " + item.optionid; string label2 = "questionid = " + item.questionid; string val = "questionresponse = " + item.questionresponse; console.writeline(label); console.writeline(label2); console.writeline(val); console.writeline(""); } console.writeline("press come in continue..."); console.readline(); console.writeline(""); console.writeline(""); console.writeline("url values"); console.writeline(""); foreach (var item in result.data[0].surveyurls) { string label = "surveyurlid = " + item.surveyurlid; string label2 = "name = " + item.name; string val = "value = " + item.value; console.writeline(label); console.writeline(label2); console.writeline(val); console.writeline(""); } console.writeline("press come in continue..."); console.readline(); console.writeline(""); console.writeline(""); console.writeline("geo values"); console.writeline(""); foreach (var item in result.data[0].surveygeodatas) { string label = "surveygeodataid = " + item.surveygeodataid; string label2 = "name = " + item.name; string val = "value = " + item.value; console.writeline(label); console.writeline(label2); console.writeline(val); console.writeline(""); } console.writeline("press come in continue..."); console.readline(); console.writeline(""); console.writeline(""); console.writeline("variables"); console.writeline(""); foreach (var item in result.data[0].surveyvariables) { string label = "surveyvariableid = " + item.surveyvariableid; string val = "value = " + item.value; console.writeline(label); console.writeline(val); console.writeline(""); } console.writeline("press come in continue..."); console.readline(); console.writeline(""); console.writeline(""); console.writeline("variables shown"); console.writeline(""); foreach (var item in result.data[0].surveyvariableshowns) { string label = "surveyvariableshownid = " + item.surveyvariableshownid; string label2 = "name = " + item.name; string val = "value = " + item.value; console.writeline(label); console.writeline(label2); console.writeline(val); console.writeline(""); } console.writeline(""); console.writeline("press come in exit..."); console.readline();

java json

No comments:

Post a Comment