Android 从JSON数组中获取特定值
因此,我使用asynctask访问在线托管的json文件。我能够在我的Android应用程序中显示json。现在我想显示json文件中的某些值。下面是我使用的json文件Android 从JSON数组中获取特定值,android,json,android-asynctask,Android,Json,Android Asynctask,因此,我使用asynctask访问在线托管的json文件。我能够在我的Android应用程序中显示json。现在我想显示json文件中的某些值。下面是我使用的json文件 https://feeds.citibikenyc.com/stations/stations.json 例如,假设我只想显示来自这个json的id。我该怎么做 protected void onPostExecute(String result) { super.onPostExecute(result);
https://feeds.citibikenyc.com/stations/stations.json
例如,假设我只想显示来自这个json的id。我该怎么做
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd.isShowing()){
pd.dismiss();
}
txtJson.setText(result);
}
这里我在'result'中得到了完整的json值
完成任务
private class JsonTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(GetJson.this);
pd.setMessage("Please wait");
pd.setCancelable(false);
pd.show();
}
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null; //BufferedReader reads text from the input stream
try {
URL url = new URL(params[0]); // params[0] is the first value in String..params (String..params can
//have any no.of string parameters )
connection = (HttpURLConnection) url.openConnection();
connection.connect(); //not necessary.works without this.
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer(); // A string buffer is like a String, but can be modified
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
//Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
}
//Log.d("BufferTest",buffer.toString());
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) { //Here the 'result' is whatever that is returned from the
//doinbackground method (Ex: buffer.toString(); )
try {
JSONObject jsonobject = new JSONObject(result);
JSONArray jsonarray = jsonobject.getJSONArray("stationBeanList");
String id = "";
for(int i =0; i<jsonarray.length();i++){
JSONObject jsonObject2 = jsonarray.getJSONObject(i);
id = jsonObject2.getString("stationName");
txtJson.setText(id);
}
} catch (JSONException e) {
e.printStackTrace();
}
super.onPostExecute(result);
if (pd.isShowing()){
pd.dismiss();
}
//txtJson.setText(result);
}
}
私有类JsonTask扩展了异步任务{
受保护的void onPreExecute(){
super.onPreExecute();
pd=newprogressdialog(GetJson.this);
pd.setMessage(“请稍候”);
pd.可设置可取消(假);
pd.show();
}
受保护的字符串doInBackground(字符串…参数){
HttpURLConnection=null;
BufferedReader=null;//BufferedReader从输入流读取文本
试一试{
URL URL=newurl(params[0]);//params[0]是String..params(String..params)中的第一个值
//具有任意数量的字符串参数)
connection=(HttpURLConnection)url.openConnection();
connection.connect();//不需要。如果没有此选项,则可以正常工作。
InputStream=connection.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(流));
StringBuffer buffer=new StringBuffer();//字符串缓冲区类似于字符串,但可以修改
字符串行=”;
而((line=reader.readLine())!=null){
buffer.append(第+行“\n”);
//Log.d(“Response:”,“>”+行);//在这里您将得到完整的响应
}
//Log.d(“BufferTest”,buffer.toString());
返回buffer.toString();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
if(连接!=null){
连接断开();
}
试一试{
if(读卡器!=null){
reader.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){//此处的“结果”是从
//doinbackground方法(例如:buffer.toString();)
试一试{
JSONObject JSONObject=新JSONObject(结果);
JSONArray JSONArray=jsonobject.getJSONArray(“stationBeanList”);
字符串id=“”;
对于(inti=0;i,如果您需要示例,请遵循
首先创建模型
public class StationBean{
String id;
String stationName;
StationBean() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
现在,如果要显示数据,则需要使用Recycleview适配器
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
List<StationBean> StationBean;
RVAdapter(List<StationBean> StationBean) {
this.stationBean= StationBean;
}
@Override
public StationBeanViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
StationBeanViewHolder pvh = new StationBeanViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
holder.stationName.setText(stationName.get(position).stationName);
}
@Override
public int getItemCount() {
if (stationName!= null) {
return persons.size();
}
return 0;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class StationNameViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView stationName;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
stationName = (TextView) itemView.findViewById(R.id.stationName);
}
}
}
公共类RVAdapter扩展了RecyclerView.Adapter{
列表StationBean;
RVAdapter(列表StationBean){
this.stationBean=stationBean;
}
@凌驾
public StationBeanViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.cardwiew,parent,false);
StationBeanView文件夹pvh=新的StationBeanView文件夹(v);
返回pvh;
}
@凌驾
公共无效onBindViewHolder(PersonViewHolder,内部位置){
holder.stationName.setText(stationName.get(position.stationName));
}
@凌驾
public int getItemCount(){
如果(stationName!=null){
返回人。大小();
}
返回0;
}
@凌驾
附加ToRecyclerView(RecyclerView RecyclerView)上的公共无效{
super.onAttachedToRecyclerView(recyclerView);
}
公共静态类StationNameViewHolder扩展了RecyclerView.ViewHolder{
卡德维尤简历;
TextView站点名称;
PersonViewHolder(查看项目视图){
超级(项目视图);
cv=(cardwiew)itemView.findviewbyd(R.id.cv);
stationName=(TextView)itemView.findViewById(R.id.stationName);
}
}
}
然后你的活动方写下以下内容:
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(mContext);
rv.setLayoutManager(llm);
final RVAdapter rvAdapter = new RVAdapter(personList);
rv.setAdapter(rvAdapter);
try {
JSONObject obj = new JSONObject(result);
JSONArray stationBeanListJSONArray = null;
stationBeanListJSONArray = obj .getJSONArray("stationBeanList");
for (int i = 0; i < stationBeanListJSONArray .length(); i++) {
StationBeanperson = new StationBean();
JSONObject jObj =stationBeanListJSONArray .getJSONObject(i);
person.Id = jObj.getString("id");
person.stationName=jObj.getString("stationName");
personList.add(i, person);
}
rvAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
RecyclerView rv=(RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(真);
LinearLayoutManager llm=新的LinearLayoutManager(mContext);
rv.setLayoutManager(llm);
最终RVAdapter RVAdapter=新RVAdapter(个人列表);
rv.设置适配器(rvAdapter);
试一试{
JSONObject obj=新JSONObject(结果);
JSONArray stationBeanListJSONArray=null;
stationBeanListJSONArray=obj.getJSONArray(“stationBeanList”);
对于(int i=0;i
希望这有帮助!您必须使用以下代码解析结果:
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd.isShowing()) {
pd.dismiss();
}
try {
JSONObject json = new JSONObject(result);
JSONArray jsonArray = json.getJSONArray("stationBeanList");
String value = "";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
value = jsonObj.getString("stationName");
Log.d("TAG", "stationName :" + value);
}
} catch (JSONException e) {
e.printStackTrace();
}
// txtJson.setText(result);
}
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
if(pd.isShowing()){
pd.解散();
}
试一试{
JSONObject json=新的JSONObject(结果);
JSONArray JSONArray=json.getJSONArray(“stationBeanList”);
字符串值=”;
for(int i=0;i