Android Studio(Json)

创建json文件

/main >> 右键new >> Folder >> Assets Folder

json对象和数组

json对象:{key1:value1,key2:value2,...}

json数组:[{key1:value1,...},{key11:value11,...}]

读取解析

java 复制代码
// 两种方式:
	org.json:Android SDK自提供的,通过JSONObject和JSONArray两个类完成
	Gson:由Google公司提供,需下载gson.jar并添加到项目中

// 1. 解析json对象:
JSONObject jsonObject = new JSONObject(myJson);
String name = jsonObject.optString("name");
int age = jsonObject.optInt("age");
boolean isMarried = jsonObject.optBoolean("married");


// 2. 解析json数组:
JSONArray jsonArray = new JSONArray(myJson);
for (int i=0; i<jsonArray.lenth(); ++){
	JSONObject jsonObject = jsonArray.getJSONObject(i);
	String name = jsonObject.optString("name");
	int age = jsonObject.optInt("age");
}

示例代码

java 复制代码
package com.example.test;

import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class JsonParse {
    // 单例
    private static JsonParse instance;
    public static JsonParse getInstance(){
        if (instance == null){
            instance = new JsonParse();
        }
        return instance;
    }
    // 读取json文件,将json数据转换为字符串
    private String read(InputStream inputStream){
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = null;
        try{
            while ((line=bufferedReader.readLine())!=null){
                stringBuilder.append(line);
                stringBuilder.append("\n");
            }
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }finally {
            try {
                if(inputStream==null){
                    inputStream.close();
                }
                if(bufferedReader==null){
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuilder.toString();
    }
    
    public List<Weather> getInfoFromJson(Context context){
        List<Weather> weathers = new ArrayList<>();
        InputStream inputStream = null;
        try {
            inputStream = context.getResources().getAssets().open("weather.json");
            String results = read(inputStream);
            Gson gson = new Gson();
            Type type = new TypeToken<List<Weather>>(){}.getType();
            weathers = gson.fromJson(results, type);
            return weathers;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
相关推荐
小菜yh1 分钟前
关于Redis
java·数据库·spring boot·redis·spring·缓存
宇卿.8 分钟前
Java键盘输入语句
java·开发语言
浅念同学8 分钟前
算法.图论-并查集上
java·算法·图论
立志成为coding大牛的菜鸟.21 分钟前
力扣1143-最长公共子序列(Java详细题解)
java·算法·leetcode
鱼跃鹰飞21 分钟前
Leetcode面试经典150题-130.被围绕的区域
java·算法·leetcode·面试·职场和发展·深度优先
爱上语文2 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
serve the people2 小时前
springboot 单独新建一个文件实时写数据,当文件大于100M时按照日期时间做文件名进行归档
java·spring boot·后端
qmx_073 小时前
HTB-Jerry(tomcat war文件、msfvenom)
java·web安全·网络安全·tomcat
为风而战3 小时前
IIS+Ngnix+Tomcat 部署网站 用IIS实现反向代理
java·tomcat
Dingdangr5 小时前
Android中的Intent的作用
android