Ensar Turkoglu
Ensar Turkoglu

Reputation: 124

Android- Parsing Local JSON Data (assets folder)

I am making a test app with Android. I have a json file and i want to read it into activity class. Here is the json file. Wrote the code but the code doesn't recognize "question" variable i 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 is my 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 from 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();
		} finally {
			try {
				br.close(); // stop reading
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		// Try to parse JSON
		try {
			// Creating JSONObject from String
			JSONObject jsonObjMain = new JSONObject();

			// Creating JSONArray from JSONObject
			JSONArray jsonArray = jsonObjMain.getJSONArray("questions");

			// JSONArray has four JSONObject
			for (int i = 0; i < jsonArray.length(); i++) {

				// Creating JSONObject from JSONArray
				JSONObject jsonObj = jsonArray.getJSONObject(i);

				// Getting data from 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 i get rid of that matter? And is the code this way making any sense? Will it work ?

The error : "question cannot be resolved to a variable" (The comment line i wrote at the bottom)


EDIT:SOLVED I wouldn't ever expect but after correcting json file (there should've been a comma which i'ven't noticed, i was gotta change the id value which have been described as int and given in the json file with a char (like a200). After that the problem was gone and the json was fetched.

Upvotes: 1

Views: 5970

Answers (1)

Simas
Simas

Reputation: 44118

You create a new JSONObject, instead of parsing your file:

JSONObject jsonObjMain = new JSONObject();

should be:

JSONObject jsonObjMain = new JSONObject(sb.toString());

Edit:

Now I understood your question.

You are out of scope and variable question doesn't exist anymore. Furthermore there are multiple questions, how do you expect to set it to your TextView?

You can save questions into an array and use any of them like this:

List<String> questions = new ArrayList<>();

try {
    ...
    for (int i = 0; i < 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 the first question
    txt_questionAdjective.setText(questions.get(0));
}

Upvotes: 2

Related Questions