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;
    }
}
相关推荐
w139548564223 分钟前
鸿蒙实战:报告与雷达图 ReportDao
android·华为·harmonyos·鸿蒙系统
人道领域22 分钟前
【0-1的agent进阶篇】Prompt 与上下文工程
java·开发语言·prompt·mcp
爱笑鱼41 分钟前
Handler(二):MessageQueue 为什么不是普通队列?
android
KINGSEA_1681 小时前
AgentScope Java 2.0 架构解析
java
阿巴斯甜1 小时前
Android Studio 新版 Logcat
android
折哥的程序人生 · 物流技术专研1 小时前
Java 23 种设计模式:从踩坑到精通 | 备忘录模式 —— 快照与撤销,给对象装一个“后悔药”
java·设计模式·备忘录模式·行为型模式·java设计模式·从踩坑到精通·撤销重做
监督者修1 小时前
从零构建 GIS 数据引擎:方案驱动架构的设计与实践
android·架构·kotlin
ikun_文1 小时前
Java里面异常Exception体系知识点
java
Yoke1 小时前
从零到三端:用 Kotlin Multiplatform + Compose 构建跨平台财务追踪应用
android
不负岁月无痕1 小时前
STL -- C++ 红黑树封装 Map 和 Set
java·c语言·开发语言·c++·面试