Android 解析json内容并在文本视图中查看
我是android的新手,它设计了一个应用程序,可以从json文件中读取问题和选项,并在文本视图中设置文本,只有单击正确的选项,下一个问题才会显示出来,但我不知道该怎么做Android 解析json内容并在文本视图中查看,android,json,Android,Json,我是android的新手,它设计了一个应用程序,可以从json文件中读取问题和选项,并在文本视图中设置文本,只有单击正确的选项,下一个问题才会显示出来,但我不知道该怎么做 { "questions": [ { "question": "Question....", "opt1": "ravi@gmail.com", "opt2": "country", "opt3" : "male",
{
"questions": [
{
"question": "Question....",
"opt1": "ravi@gmail.com",
"opt2": "country",
"opt3" : "male",
"opt4": "option 4",
"coropt": "b"
},
{
"question": "Question....",
"opt1": "johnny_depp@gmail.com",
"opt2": "country",
"opt3" : "male",
"opt4": "option 4",
"coropt": "a"
},
{
"question": "Question....",
"opt1": "leonardo_dicaprio@gmail.com",
"opt2": "country",
"opt3" : "male",
"opt4": "option 4",
"coropt": "d"
},
{
"question": "Question....",
"opt1": "john_wayne@gmail.com",
"opt2": "country",
"opt3" : "male",
"opt4": "option 4",
"coropt": "c"
}
]
}
这是我正在编写的代码,但我被困在如何以及在何处解析json数据的问题上
public class JavaQuiz extends AppCompatActivity {
TextView ltv, tv1, tv2, tv3, tv4;
// JSON Node names
private static final String TAG_QUESTIONS = "questions";
private static final String TAG_QUESTION = "question";
private static final String TAG_OPTION1 = "opt1";
private static final String TAG_OPTION2 = "opt2";
private static final String TAG_OPTION3 = "opt3";
private static final String TAG_OPTION4 = "opt4";
private static final String TAG_CORRECTOPTION = "coropt";
// contacts JSONArray
JSONArray questions = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> questionList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_quiz);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ltv = (TextView) findViewById(R.id.textView7);
tv1 = (TextView) findViewById(R.id.textView8);
tv2 = (TextView) findViewById(R.id.textView9);
tv3 = (TextView) findViewById(R.id.textView10);
tv4 = (TextView) findViewById(R.id.textView11);
}
class QuestionShow extends AsyncTask<String, Void, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(JavaQuiz.this);
progressDialog.setTitle("Fetching");
progressDialog.setMessage("Setting question");
progressDialog.show();
}
@Override
protected Void doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
/* HttpPost post=new HttpPost("http://192.168.1.5/questions.json");
post.setEntity(new UrlEncodedFormEntity(params[0]));
HttpResponse response = client.execute(post);
*/
String jsonStr = "http://192.168.1.5/questions.json";
/* if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
questions = jsonObj.getJSONArray(TAG_QUESTIONS);
// looping through All questions
for (int i = 0; i < questions.length(); i++) {
JSONObject q = questions.getJSONObject(i);
String question = q.getString(TAG_QUESTION);
String op1 = q.getString(TAG_OPTION1);
String op2 = q.getString(TAG_OPTION2);
String op3 = q.getString(TAG_OPTION3);
String op4 = q.getString(TAG_OPTION4);
String coropt = q.getString(TAG_CORRECTOPTION);
/* // tmp hashmap for single question
HashMap<String, String> ques = new HashMap<String, String>();
// adding each child node to HashMap key => value
ques.put(TAG_QUESTION, question);
ques.put(TAG_OPTION1, op1);
ques.put(TAG_OPTION2, op2);
ques.put(TAG_OPTION3, op3);
ques.put(TAG_OPTION4, op4);
ques.put(TAG_CORRECTOPTION, coropt);
// adding contact to contact list
questionList.add(ques);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
*/
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
String jsonStr = "http://192.168.1.5/questions.json";
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
questions = jsonObj.getJSONArray(TAG_QUESTIONS);
// looping through All questions
for (int i = 0; i < questions.length(); ) {
JSONObject q = questions.getJSONObject(i);
String question = q.getString(TAG_QUESTION);
String op1 = q.getString(TAG_OPTION1);
String op2 = q.getString(TAG_OPTION2);
String op3 = q.getString(TAG_OPTION3);
String op4 = q.getString(TAG_OPTION4);
String coropt = q.getString(TAG_CORRECTOPTION);
ltv.setText(question);
tv1.setText(op1);
tv2.setText(op1);
tv3.setText(op1);
tv4.setText(op1);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
public void answer(View view)
{
String
if (view==tv1)
{
if(tv1.getText().toString()==)
}
}
}
public类javaquick扩展了appcompative活动{
TextView ltv、tv1、tv2、tv3、tv4;
//JSON节点名称
私有静态最终字符串标记_QUESTIONS=“QUESTIONS”;
私有静态最终字符串标记_QUESTION=“QUESTION”;
私有静态最终字符串标记_OPTION1=“opt1”;
私有静态最终字符串标记_OPTION2=“opt2”;
私有静态最终字符串标记_OPTION3=“opt3”;
私有静态最终字符串标记_OPTION4=“opt4”;
私有静态最终字符串标记_CORRECTOPTION=“coropt”;
//联系JSONArray
JSONArray问题=null;
//ListView的Hashmap
ArrayList问题列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u java\u测验);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
ltv=(TextView)findViewById(R.id.textView7);
tv1=(TextView)findViewById(R.id.textView8);
tv2=(TextView)findViewById(R.id.textView9);
tv3=(TextView)findViewById(R.id.textView10);
tv4=(TextView)findViewById(R.id.textView11);
}
类QuestionShow扩展异步任务{
进行对话进行对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=新建progressDialog(javaquick.this);
progressDialog.setTitle(“获取”);
progressDialog.setMessage(“设置问题”);
progressDialog.show();
}
@凌驾
受保护的Void doInBackground(字符串…参数){
HttpClient=new DefaultHttpClient();
/*HttpPost=新的HttpPost(“http://192.168.1.5/questions.json");
post.setEntity(新的UrlEncodedFormEntity(参数[0]);
HttpResponse response=client.execute(post);
*/
字符串jsonStr=”http://192.168.1.5/questions.json";
/*if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
questions=jsonObj.getJSONArray(TAG_questions);
//循环回答所有问题
对于(int i=0;ivalue
提问(附加问题、问题);
问题。付诸表决(标签选项1,op1);
问题。放置(标签选项2、选项2);
问题。提出(标签选项3,op3);
问题。提出(标签选项4、选项4);
问题。出售(TAG_Correct Option,coropt);
//将联系人添加到联系人列表
问题列表。添加(问题);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
*/
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
字符串jsonStr=”http://192.168.1.5/questions.json";
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
questions=jsonObj.getJSONArray(TAG_questions);
//循环回答所有问题
for(int i=0;i
首先要运行AsyncTask,需要从onCreate方法调用AsyncTask,如下所示
public class JavaQuiz extends AppCompatActivity {
TextView ltv, tv1, tv2, tv3, tv4;
// contacts JSONArray
JSONArray questions = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> questionList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_quiz);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ltv = (TextView) findViewById(R.id.textView7);
tv1 = (TextView) findViewById(R.id.textView8);
tv2 = (TextView) findViewById(R.id.textView9);
tv3 = (TextView) findViewById(R.id.textView10);
tv4 = (TextView) findViewById(R.id.textView11);
String jsondata = "{\n" +
"\"questions\": [\n" +
" {\n" +
" \"question\": \"Question....\",\n" +
" \"opt1\": \"ravi@gmail.com\",\n" +
" \"opt2\": \"country\",\n" +
" \"opt3\" : \"male\",\n" +
" \"opt4\": \"option 4\",\n" +
" \"coropt\": \"b\"\n" +
" },\n" +
" {\n" +
" \"question\": \"Question....\",\n" +
" \"opt1\": \"johnny_depp@gmail.com\",\n" +
" \"opt2\": \"country\",\n" +
" \"opt3\" : \"male\",\n" +
" \"opt4\": \"option 4\",\n" +
" \"coropt\": \"a\"\n" +
" },\n" +
" {\n" +
" \"question\": \"Question....\",\n" +
" \"opt1\": \"leonardo_dicaprio@gmail.com\",\n" +
" \"opt2\": \"country\",\n" +
" \"opt3\" : \"male\",\n" +
" \"opt4\": \"option 4\",\n" +
" \"coropt\": \"d\"\n" +
" },\n" +
" {\n" +
" \"question\": \"Question....\",\n" +
" \"opt1\": \"john_wayne@gmail.com\",\n" +
" \"opt2\": \"country\",\n" +
" \"opt3\" : \"male\",\n" +
" \"opt4\": \"option 4\",\n" +
" \"coropt\": \"c\"\n" +
" }\n" +
"]\n" +
"}";
new QuestionShow(JavaQuiz.this).execute(jsondata);
//If using URL use this below
// new QuestionShow(JavaQuiz.this).execute("http://192.168.1.5/questions.json");
}
public class QuestionShow extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
ProgressDialog progressDialog;
Context context;
public QuestionShow(Context context) {
this.context = context;
}
// JSON Node names
public String TAG_QUESTIONS = "questions";
public String TAG_QUESTION = "question";
public String TAG_OPTION1 = "opt1";
public String TAG_OPTION2 = "opt2";
public String TAG_OPTION3 = "opt3";
public String TAG_OPTION4 = "opt4";
public String TAG_CORRECTOPTION = "coropt";
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("Fetching");
progressDialog.setMessage("Setting question");
progressDialog.show();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
/*Your networking activities can be done here and the params[0] URL can be passed to it*/
// HttpClient client = new DefaultHttpClient();
// HttpPost post = new HttpPost("http://192.168.1.5/questions.json");
// try {
// post.setEntity(new UrlEncodedFormEntity(params[0]));
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
// HttpResponse response = client.execute(post);
//
String jsonStr = params[0];
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
questions = jsonObj.getJSONArray(TAG_QUESTIONS);
questionList = new ArrayList<>();
// looping through All questions
for (int i = 0; i < questions.length(); i++) {
JSONObject q = questions.getJSONObject(i);
String question = q.getString(TAG_QUESTION);
String op1 = q.getString(TAG_OPTION1);
String op2 = q.getString(TAG_OPTION2);
String op3 = q.getString(TAG_OPTION3);
String op4 = q.getString(TAG_OPTION4);
String coropt = q.getString(TAG_CORRECTOPTION);
// tmp hashmap for single question
HashMap<String, String> ques = new HashMap<String, String>();
// adding each child node to HashMap key => value
ques.put(TAG_QUESTION, question);
ques.put(TAG_OPTION1, op1);
ques.put(TAG_OPTION2, op2);
ques.put(TAG_OPTION3, op3);
ques.put(TAG_OPTION4, op4);
ques.put(TAG_CORRECTOPTION, coropt);
// adding contact to contact list
questionList.add(ques);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// }
return questionList;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> hashMapArrayList) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (hashMapArrayList.size() > 0) {
ltv.setText(hashMapArrayList.get(2).get(TAG_QUESTION));
tv1.setText(hashMapArrayList.get(2).get(TAG_OPTION1));
tv2.setText(hashMapArrayList.get(2).get(TAG_OPTION2));
tv3.setText(hashMapArrayList.get(2).get(TAG_OPTION3));
tv4.setText(hashMapArrayList.get(2).get(TAG_OPTION4));
} else {
Toast.makeText(getApplicationContext(), "No data found", Toast.LENGTH_SHORT).show();
}
}
}
// public void answer(View view)
// {
// String
// if (view==tv1)
// {
// if(tv1.getText().toString()==)
// }
// }
}
public类javaquick扩展了appcompative活动{
TextView ltv、tv1、tv2、tv3、tv4;
//联系JSONArray
JSONArray问题=null;
//ListView的Hashmap
ArrayList问题列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u java\u测验);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
ltv=(TextView)findViewById(R.id.textView7);
tv1=(TextView)findViewById(R.id.textView8);
tv2=(TextView)findViewById(R.id.textView9);
tv3=(文本视图)findViewById(R