Java - extracting a value from a JSON file -
this question has answer here:
- how extract value json 4 answers
i have following json text file:
{ "quiz": { "q1": { "question": "is test?", "options": [ "test a", "test b", "test c", "test d" ], "answer": "test a" }, "q2": { "question": "are sure?", "options": [ "test a", "test b", "test c", "test d" ], "answer": "test c" } } } i trying write program able take values file , display them in gui.
at moment have following:
jsonparser parser = new jsonparser(); try { object obj = parser.parse(new filereader("/users/testing/desktop/workspace/test1.txt")); jsonobject jsonobject = (jsonobject) obj; } catch (exception e) { e.printstacktrace(); } how go getting value "q1" "question"? understand in js use $.quiz.q1.question im unsure how go in java.
jsonparser parser = new jsonparser(); try { object obj = parser.parse(new filereader("/users/testing/desktop/workspace/test1.txt")); jsonobject jsonobject = new jsonobject(obj.tostring);<-----change jsonobject que1obj = jsonobject.getjsonobject("q1")<------ string questiontitle = que1obj.getstring("question"); jsonarray questionsarray =que1obj.getjsonarray("options"); string answer = que1obj.getstring("answer"); } catch (exception e) { e.printstacktrace(); }
Comments
Post a Comment