java - (SOLVED) Android- Parsing Local JSON Data (assets folder) -


i making test app android. have json file , want read activity class. here json file. wrote code code doesn't recognize "question" variable described upside.

{      "questions": [          {                  "id": "a200",                  "question": "lorem ipsum dolor _______ amet",                  "optiona": "lorem",                  "optionb": "ipsum",                  "optionc" : "dolor",                  "optiond": "sit"                  "rightanswer" : "sit"                                },          {                  "id": "b200",                  "question": "_____ ipsum dolor sit amet",                  "optiona": "lorem",                  "optionb": "ipsum",                  "optionc" : "dolor",                  "optiond": "sit"                  "rightanswer" : "lorem"                                },          {                  "id": "c200",                  "question": "lorem _____ dolor sit amet",                  "optiona": "lorem",                  "optionb": "ipsum",                  "optionc" : "dolor",                  "optiond": "sit"                  "rightanswer" : "ipsum"                              },    ]  }

and here quizactivityadjectives.java file's oncreate part.

public class quizactivityadjectives extends activity {    	textview txt_questionadjective;    	@override  	protected void oncreate(bundle savedinstancestate) {  		super.oncreate(savedinstancestate);  		setcontentview(r.layout.activity_quiz_adjectives);  		// hangi xml dosyasının dikkate alınacağı belirlendi.    		// reading json file assets folder  		stringbuffer sb = new stringbuffer();  		bufferedreader br = null;  		try {  			br = new bufferedreader(new inputstreamreader(getassets().open(  					"questions.json")));  			string temp;  			while ((temp = br.readline()) != null)  				sb.append(temp);  		} catch (ioexception e) {  			e.printstacktrace();  		} {  			try {  				br.close(); // stop reading  			} catch (ioexception e) {  				e.printstacktrace();  			}  		}    		// try parse json  		try {  			// creating jsonobject string  			jsonobject jsonobjmain = new jsonobject();    			// creating jsonarray jsonobject  			jsonarray jsonarray = jsonobjmain.getjsonarray("questions");    			// jsonarray has 4 jsonobject  			for (int = 0; < jsonarray.length(); i++) {    				// creating jsonobject jsonarray  				jsonobject jsonobj = jsonarray.getjsonobject(i);    				// getting data individual jsonobject  				int id = jsonobj.getint("id");  				string question = jsonobj.getstring("question");  				string optiona = jsonobj.getstring("optiona");  				string optionb = jsonobj.getstring("optionb");  				string optionc = jsonobj.getstring("optionc");  				string optiond = jsonobj.getstring("optiond");  				string rightanswer = jsonobj.getstring("rightanswer");    			}    		} catch (jsonexception e) {  			// todo auto-generated catch block  			e.printstacktrace();  		}    		string myjsonstring = sb.tostring();    		txt_questionadjective = (textview) findviewbyid(r.id.txt_questionadjective);  		txt_questionadjective.settext(question); //the code errors here. can not resolve question variable.    	}

how can rid of matter? , code way making sense? work ?

the error : "question cannot resolved variable" (the comment line wrote @ bottom)

thanks in advance.

edit:solved wouldn't ever expect after correcting json file (there should've been comma i'ven't noticed, gotta change id value have been described int , given in json file char (like a200). after problem gone , json fetched. lot consideration.

you create new jsonobject, instead of parsing file:

jsonobject jsonobjmain = new jsonobject(); 

should be:

jsonobject jsonobjmain = new jsonobject(sb.tostring()); 

edit:

now understood question.

you out of scope , variable question doesn't exist anymore. furthermore there multiple questions, how expect set textview?

you can save questions array , use of them this:

list<string> questions = new arraylist<>();  try {     ...     (int = 0; < jsonarray.length(); i++) {         ...         questions.add(jsonobj.getstring("question"));         ...     }  } catch (jsonexception e) {     // todo auto-generated catch block     e.printstacktrace(); }  string myjsonstring = sb.tostring();  // check if questions have been fetched if (questions.size() > 0) {     txt_questionadjective = (textview) findviewbyid(r.id.txt_questionadjective);     // use first question     txt_questionadjective.settext(questions.get(0)); } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -