这里我的应用程序是当我输入17603号火车编辑文本并输入按钮时。我将更正它,从assets文件夹获取traindetails.txt,并转换为字符串然后将字符串传递给json数组。它将在logcat中显示。在这里,我没有收到任何错误。
JSON Requset存储在Assets文件夹中的.txt位置
{
"status":"OK",
"result":{
"trainno":"17603",
"route":[
{
"code":"KCG",
"name":"Kacheguda",
"arr":"First",
"dep":"21:00",
"day":1,
"stop":"Y",
"dts":"2.5"
}
]
}
}
主要活动
package com.example.trainroutes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private EditText train_search_editText;
private Button submit_button;
private ListView train_listview;
// private String jsonStirng ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
train_search_editText = (EditText) findViewById(R.id.trainSearch);
submit_button = (Button) findViewById(R.id.search_train_button);
train_listview = (ListView) findViewById(R.id.trian_name_listview);
submit_button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (train_search_editText.getText().toString().equals("17603")) {
try {
// Reading text file from the assets folder
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferReader = null;
try {
bufferReader = new BufferedReader(new InputStreamReader(
getAssets().open("traindetails.txt")));
String temp;
while ((temp = bufferReader.readLine()) != null) {
stringBuffer.append(temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String jsonStirng = stringBuffer.toString();
Log.e("Json String", "JSON String"+jsonStirng);
// creating jsonobject from string
JSONObject jsonMainObj = new JSONObject(jsonStirng);
// creating json array from json object
JSONArray jsonArrayObject = jsonMainObj.getJSONArray("route");
for (int i = 0; i < jsonArrayObject.length(); i++) {
JSONObject jsonObject = jsonArrayObject.getJSONObject(i);
// getting data from individual object
String code = jsonObject.getString("code");
String name = jsonObject.getString("name");
String arr = jsonObject.getString("arr");
String dep = jsonObject.getString("dep");
int day = jsonObject.getInt("day");
String stop = jsonObject.getString("stop");
String dts = jsonObject.getString("dts");
Log.d("JSONObject", "train " + name.toString());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "Invalid ",
Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:1)
这是你应该怎么做
String jsonString = loadJSONFromAsset(getApplicationContext());
try {
JSONObject jsonMainObj = new JSONObject(jsonString);
JSONObject jsonArrayObjectResult = jsonMainObj.getJSONObject("result");
JSONArray jsonArrayObjectRoute = jsonArrayObjectResult.getJSONArray("route");
for (int i = 0; i < jsonArrayObjectRoute.length(); i++) {
JSONObject jsonObject = jsonArrayObjectRoute.getJSONObject(i);
// getting data from individual object
String code = jsonObject.getString("code");
String name = jsonObject.getString("name");
String arr = jsonObject.getString("arr");
String dep = jsonObject.getString("dep");
int day = jsonObject.getInt("day");
String stop = jsonObject.getString("stop");
String dts = jsonObject.getString("dts");
Log.d("JSONObject", "train " + name.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
public String loadJSONFromAsset(Context mContext) {
String json = null;
try {
InputStream is = mContext.getAssets().open("test.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
答案 1 :(得分:0)
使用此代码
package com.example.trainroutes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private EditText train_search_editText;
private Button submit_button;
private ListView train_listview;
// private String jsonStirng ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
train_search_editText = (EditText) findViewById(R.id.trainSearch);
submit_button = (Button) findViewById(R.id.search_train_button);
train_listview = (ListView) findViewById(R.id.trian_name_listview);
submit_button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (train_search_editText.getText().toString().equals("17603")) {
try {
// Reading text file from the assets folder
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferReader = null;
try {
bufferReader = new BufferedReader(new InputStreamReader(
getAssets().open("traindetails.txt")));
String temp;
while ((temp = bufferReader.readLine()) != null) {
stringBuffer.append(temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String jsonStirng = stringBuffer.toString();
Log.e("Json String", "JSON String"+jsonStirng);
// creating jsonobject from string
JSONObject jsonMainObj = new JSONObject(jsonStirng);
JSONObject jsonresultObject=jsonMainObj.getJsonObject("result")
// creating json array from json object
JSONArray jsonArrayObject = jsonresultObject.getJSONArray("route");
for (int i = 0; i < jsonArrayObject.length(); i++) {
JSONObject jsonObject = jsonArrayObject.getJSONObject(i);
// getting data from individual object
String code = jsonObject.getString("code");
String name = jsonObject.getString("name");
String arr = jsonObject.getString("arr");
String dep = jsonObject.getString("dep");
int day = jsonObject.getInt("day");
String stop = jsonObject.getString("stop");
String dts = jsonObject.getString("dts");
Log.d("JSONObject", "train " + name.toString());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "Invalid ",
Toast.LENGTH_SHORT).show();
}
}
}