Android Studio 如何使用个人json文件(详细)

1.在assetc里面创建json数据 

在res里面右边new找到folder然后创建assets 创建一个.json文件

[
  {
    "id": 1,
    "username": "学生1",
    "phone": "123456",
    "email": "123@163.com"
  },
  {
    "id": 2,
    "username": "学生2",
    "phone": "156984",
    "email": "234@163.com"
  },
  {
    "id": 3,
    "username": "学生3",
    "phone": "21311",
    "email": "345@163.com"
  },
  {
    "id": 4,
    "username": "学生4",
    "phone": "12315",
    "email": "567@163.com"
  },
  {
    "id": 5,
    "username": "学生5",
    "phone": "1233",
    "email": "789@163.com"
  },
  {
    "id": 6,
    "username": "学生6",
    "phone": "77182",
    "email": "890@163.com"
  },
  {
    "id": 7,
    "username": "学生7",
    "phone": "14234",
    "email": "890@163.com"
  },
  {
    "id": 8,
    "username": "学生8",
    "phone": "77182",
    "email": "123@163.com"
  }
]

2.写一个listview准备将json数据放入列表里面

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

3.创建一个Bean类

咱们所需三个姓名 电话 邮箱

package com.example.main1;

import java.io.Serializable;

public class ClassBean implements Serializable {
    private String name;
    private String email;
    private int phone;

    public ClassBean(String name, String email, int phone) {
        this.name = name;
        this.email = email;
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public String getName() {
        return name;
    }

    public int getPhone() {
        return phone;
    }
}

4.创建一个xml子布局(用于显示json数据内容)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/cheek"
        android:textSize="15sp"
        android:text="姓名"
        android:gravity="center"
        android:id="@+id/name"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/cheek"
        android:textSize="15sp"
        android:text="姓名"
        android:gravity="center"
        android:id="@+id/phone"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/cheek"
        android:textSize="15sp"
        android:text="姓名"
        android:gravity="center"
        android:id="@+id/email"/>
</LinearLayout>
</LinearLayout>

5.创建一个BaseAdapter适配器

package com.example.main1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class JsonAdapter extends BaseAdapter {
    private List<ClassBean> list;
    private Context context;

    public JsonAdapter(List<ClassBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.classiteam,parent,false);
        }
        ClassBean bean=list.get(position);
        TextView name=convertView.findViewById(R.id.name);
        TextView phone=convertView.findViewById(R.id.phone);
        TextView email=convertView.findViewById(R.id.email);

        name.setText(bean.getName());
        phone.setText(bean.getPhone()+"");
        email.setText(bean.getEmail());

        return convertView;
    }
}

6. 获取json数据,将数据放入listview列表里面

package com.example.main1;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

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

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class JsonActivity3 extends AppCompatActivity {

    private ListView list;
    private List<ClassBean>data=new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json3);

        findcheck();
        try {
            // 从 assets 中读取 JSON 数据
            InputStream inputStream = getAssets().open("class.json");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();

            // 将 JSON 数据解析成 JSONArray 对象
            JSONArray jsonArray = new JSONArray(new String(buffer, "UTF-8"));

            // 遍历 JSONArray,获取每个对象的属性值
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String name = jsonObject.getString("username");
                int phone = jsonObject.getInt("phone");
                String email=jsonObject.getString("email");

                ClassBean bean=new ClassBean(name,email,phone);
                data.add(bean);
                list.setAdapter(new JsonAdapter(data, JsonActivity3.this));
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        ClassBean bean1=data.get(position);
                        Toast.makeText(JsonActivity3.this, bean1.getName(), Toast.LENGTH_SHORT).show();
                    }
                });

                // ...
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }





    }
    private void findcheck(){
        list=findViewById(R.id.list);
        data=new ArrayList<>();
    }
}

6.效果图展示

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 Android Studio 中访问本地 JSON 文件,可以使用以下代码: ```java // 读取 JSON 文件 String jsonString = null; try { InputStream inputStream = getAssets().open("file.json"); int size = inputStream.available(); byte[] buffer = new byte[size]; inputStream.read(buffer); inputStream.close(); jsonString = new String(buffer, "UTF-8"); } catch (IOException e) { e.printStackTrace(); } // 将 JSON 字符串转换为 JSONObject try { JSONObject jsonObject = new JSONObject(jsonString); // 从 jsonObject 中获取需要的信息 } catch (JSONException e) { e.printStackTrace(); } ``` 在上述代码中,我们首先使用 `getAssets().open("file.json")` 从 assets 文件夹中打开 JSON 文件,然后读取其中的内容,并将其转换为 JSON 对象。如果你的 JSON 文件不在 assets 文件夹中,可以使用 `FileInputStream` 来读取本地文件,例如: ```java // 读取 JSON 文件 String jsonString = null; try { FileInputStream inputStream = new FileInputStream(new File("/sdcard/file.json")); int size = inputStream.available(); byte[] buffer = new byte[size]; inputStream.read(buffer); inputStream.close(); jsonString = new String(buffer, "UTF-8"); } catch (IOException e) { e.printStackTrace(); } // 将 JSON 字符串转换为 JSONObject try { JSONObject jsonObject = new JSONObject(jsonString); // 从 jsonObject 中获取需要的信息 } catch (JSONException e) { e.printStackTrace(); } ``` 在这个例子中,我们使用 `FileInputStream` 从外部存储器中读取 JSON 文件。请注意,如果你的应用需要读取外部存储器中的文件,你需要在 AndroidManifest.xml 文件中添加读取外部存储器的权限: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小舒卿雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值