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;
    }
}
相关推荐
怒放吧德德3 小时前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆4 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
火柴就是我5 小时前
让我们实现一个更好看的内部阴影按钮
android·flutter
心之语歌7 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊8 小时前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang8 小时前
用六边形架构与整洁架构对比是伪命题?
java·架构
Ray Liang9 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Java水解10 小时前
Java 中间件:Dubbo 服务降级(Mock 机制)
java·后端
砖厂小工12 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心12 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能