首页 > 解决方案 > 如何在 textview 中显示来自 json 的特定条目?

问题描述

我有一个包含一个编辑文本、按钮和 Textview 的活动。现在我想如果有人在编辑文本中输入 Java,那么它的信息将被填充到文本视图中。这是我的 JSON。我还包括了我的 .java 文件。

{
  "compscience": [
    {
      "bookname": "java",
      "row": "1",
      "column": "1"
    },
    {
      "bookname": "cns",
      "row": "2",
      "column": "2"
    },
    {
      "bookname": "rdbms",
      "row": "3",
      "column": "3"
    },
    {
      "bookname": "daa",
      "row": "4",
      "column": "4"
    }
  ]
}

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class act1 extends AppCompatActivity {
    EditText subname;
    Button fetch;
    static String ab,single="";
    public static   String c;
   static TextView data;
fetchdata process;
    StringBuffer response ;

void  init()
{
    subname = findViewById(R.id.subname);
    ab = subname.toString();
    fetch = findViewById(R.id.button);
    data=findViewById(R.id.result);      
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_act1);
    init();
}
public  void fetch(View view)
{
   process = new fetchdata();
    process.execute();//it will start do in background
}

 class fetchdata extends AsyncTask {

    @Override
    protected Object doInBackground(Object[] objects) {
        response = new StringBuffer();
        try {
            URL url = new URL("https://api.myjson.com/bins/c1054");

            URLConnection connection =url.openConnection();

            InputStream inputStream = connection.getInputStream();
            InputStreamReader reader = new InputStreamReader(inputStream);
            BufferedReader buffer = new BufferedReader(reader);
            String line = "";

            while((line = buffer.readLine()) != null) {
                response.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);

        try {
            JSONArray array=new JSONArray("respnse");

            for(int i=0;i<array.length();i++)
            {
                JSONObject obj=array.getJSONObject(i);

                if(ab.equals(obj.getString("booknmae")))
                {
                    single = single +obj.getString("row");
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        act1.data.setText(single);
    }
}
}

标签: androidjson

解决方案


首先,您需要了解如何JSONObjectJSONArray工作,您的 json 响应是JSONObject并且其中有一个名称为值的compscienceJSONArray

尝试这个:

try {
        JSONObject respJson = new JSONObject(response.toString());
        JSONArray array = respJson.getJSONArray("compscience");
        for(int i=0;i<array.length();i++)
        {
            JSONObject obj=array.getJSONObject(i);

            if(ab.equals(obj.getString("booknmae")))
            {

                single = single +obj.getString("row");

            }
        }


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

推荐阅读