Hi Guys im new in android development please help me to read all json data in to android and how to put in ArrayList ..here i have 4 set of questions (4 x 4) = 16 questions .. in one set i have 4 question i want to call 1 question randomly from set .. please help me .. Thanks in advance
package com.example.truefalsegame;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.xml.sax.SAXException;
//import com.example.jsonparser.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity
{
SAXParserFactory factory;
SAXParser saxParser;
String datas;
String SrData = "";
String queData = "";
String ansData = "";
String des;
TextView srNo_txt, questions_txt, answer_txt;
ImageView imageView;
ArrayList<String> list = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
questions_txt = (TextView) findViewById(R.id.QuestionTxt);
imageView = (ImageView)findViewById(R.id.imageView1);
factory = SAXParserFactory.newInstance();
try {
saxParser = factory.newSAXParser();
InputStream is = getAssets().open("concepts.json");
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String str;
StringBuffer sb=new StringBuffer();
while((str=br.readLine())!=null)
{
sb = sb.append(str);
datas =(str);
list.add(datas);
}
str = sb.toString();
JSONArray jsonarray = new JSONArray(str);
for(int i=0; i<jsonarray.length(); i++)
{
JSONObject obj = jsonarray.getJSONObject(i);
String questions = obj.getString("Questions");
JSONArray question = obj.getJSONArray("Questions");
JSONObject ids = question.getJSONObject(0);
des = ids.getString("Question");
Log.d("hitesh", "des : "+ des );
//String answers = obj.getString("Answer");
int srno = i+1;
System.out.println(questions);
//System.out.println(answers);
queData += des+" \n ";
}
questions_txt.setText(""+queData);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String append(String str) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my Json file My jsonfile name is (concepts.json) and its in Assets folder
[
{
"Concept": "1",
"Questions": [
{
"Question": "Carbon compounds are only straight chain compounds",
"Answer": "F"
},
{
"Question": "Carbon compounds are only cyclic compounds",
"Answer": "F"
},
{
"Question": "Carbon - Carbon linkage may form straight chain, branched chain and ring like or cyclic compounds",
"Answer": "T"
},
{
"Question": "Carbon compounds can't be cyclic compounds",
"Answer": "F"
}
]
},
{
"Concept": "2",
"Questions": [
{
"Question": "Saturated carbon compounds are highly reactive",
"Answer": "F"
},
{
"Question": "Unsaturated organic compounds are more reactive than saturated organic compounds",
"Answer": "T"
},
{
"Question": "Unsaturated organic compounds are less reactive than saturated organic compounds",
"Answer": "F"
},
{
"Question": "Alkanes are less reactive than alkynes",
"Answer": "T"
}
]
},
{
"Concept": "3",
"Questions": [
{
"Question": "Hydrocarbons contain only carbon and hydrogen",
"Answer": "T"
},
{
"Question": "Hydrocarbons contain only carbon",
"Answer": "F"
},
{
"Question": "A compound of carbon and oxygen can be hydrocarbon",
"Answer": "F"
},
{
"Question": "A compound of carbon and nitrogen can be hydrocarbon",
"Answer": "F"
}
]
},
{
"Concept": "3",
"Questions": [
{
"Question": "Diagram",
"Answer": "T"
},
{
"Question": "Diagram",
"Answer": "T"
},
{
"Question": "Diagram",
"Answer": "F"
},
{
"Question": "Diagram",
"Answer": "F"
}
]
}
]