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;
    }
}
相关推荐
胚芽鞘6814 小时前
关于java项目中maven的理解
java·数据库·maven
岁忧5 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
CJi0NG5 小时前
【自用】JavaSE--算法、正则表达式、异常
java
Hellyc6 小时前
用户查询优惠券之缓存击穿
java·redis·缓存
今天又在摸鱼6 小时前
Maven
java·maven
老马啸西风6 小时前
maven 发布到中央仓库常用脚本-02
java·maven
代码的余温6 小时前
MyBatis集成Logback日志全攻略
java·tomcat·mybatis·logback
coderlin_7 小时前
BI布局拖拽 (1) 深入react-gird-layout源码
android·javascript·react.js
一只叫煤球的猫7 小时前
【🤣离谱整活】我写了一篇程序员掉进 Java 异世界的短篇小说
java·后端·程序员
2501_915918418 小时前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview