在 textView 中打印一个 json 文件

Print a json file in textView

这是我的 json 文件:settings.json(在资产文件夹中):

{
  "settings": [
    {
      "level": "upper"
    }
  ]
}

我在 Android 工作室工作。我在 json 文件的 TextView 中打印了上面的字,但这不起作用,我的应用程序崩溃了。想法? 我的代码在 java 文件中:

public class MainActivity extends AppCompatActivity {
    ArrayList<String> level = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {

            JSONObject obj = new JSONObject(loadJSONFromAsset());

            JSONArray userArray = obj.getJSONArray("settings");

            for (int i = 0; i < userArray.length(); i++) {

                JSONObject userDetail = userArray.getJSONObject(i);

                level.add(userDetail.getString("level"));
            }
            } catch(JSONException e){
                e.printStackTrace();

            }
        TextView textView = (TextView) findViewById(R.id.textView);

        textView.setText(level.get(0));

        setContentView(R.layout.activity_main);
    }
    public String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = getAssets().open("settings.json");

            int size = is.available();

            byte[] buffer = new byte[size];

            is.read(buffer);

            is.close();

            json = new String(buffer, "UTF-8");


        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

谢谢。 Android, json, 源代码...

我希望这对你有用,

像这样写你的onCreate()方法,

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {

            JSONObject obj = new JSONObject(loadJSONFromAsset());
            JSONArray userArray = obj.getJSONArray("settings");
            for (int i = 0; i < userArray.length(); i++) {
                JSONObject userDetail = userArray.getJSONObject(i);
                level.add(userDetail.getString("level"));
            }
            } catch(JSONException e){
                e.printStackTrace();

            }
        TextView textView = (TextView) findViewById(R.id.textView);

        textView.setText(level.get(0));
    }

首先设置 setContentView() 方法,然后初始化您的 textview.In 在布局初始化之前访问文本视图的代码。

为什么 else 在 if 函数中起作用?该级别的记录是上层的。为什么 if 不为真?

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        try {
            // get JSONObject from JSON file
            JSONObject obj = new JSONObject(loadJSONFromAsset());
            // fetch JSONArray named users
            JSONArray userArray = obj.getJSONArray("settings");
            // implement for loop for getting users list data
            for (int i = 0; i < userArray.length(); i++) {
                // create a JSONObject for fetching single user data
                JSONObject userDetail = userArray.getJSONObject(i);
                // fetch email and name and store it in arraylist
                level.add(userDetail.getString("level"));
            }
            } catch(JSONException e){
                e.printStackTrace();

            }
        TextView textView = (TextView) findViewById(R.id.textView);

        if (level.get(0) == "upper") {
            textView.setText(level.get(0));
        } else {

        }