Android 解析本地Json数据
  HvTJUzsxOBtS 2023年11月25日 17 0



文章目录

  • 1、简介
  • 2、文件结构
  • 3、testdata.json json 文件
  • 4、MainActivity 文件
  • 5、log输出


1、简介

1)解析本地的JSON数据
2)对Json 数据的解析方法有所了解

我们使用的 是Java 原生的 Json 数据来解析

2、文件结构

Android  解析本地Json数据_json

1)testData.json 是json 文件
2)MainActivity 是 具体实现

3、testdata.json json 文件
{
  "num": 0,
  "progectName": "test",
  "data": {
    "year": 2018,
    "week": ["Sunday", "Monday", "Tuesday"],
    "boolean": true,
    "person": {
      "name": "lum",
      "age": "26",
      "hobby": "girl"
    }
  },
  "animal": [{
    "name": "dog",
    "age": "狗龄 2"
  },
    {
      "name": "cat",
      "age": "猫龄 1"
    }
  ]

}

http://www.bejson.com/

可以在这个网站检测自己写的json 是否符合规范

4、MainActivity 文件
package com.example.tssh.myjsondata;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class MainActivity extends AppCompatActivity {
private String  TAG ="MainActivity: ";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        JsonDataToString ();
    }

    private void JsonDataToString() {
            Log.i(TAG,"读取 json  文件 转化 String  ");
            InputStreamReader inputStreamReader = null;
            String  jsonData = "";
            try {
                 inputStreamReader = new InputStreamReader(getResources().openRawResource(R.raw.testdata), "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String line;
                StringBuilder stringBuilder = new StringBuilder();
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                inputStreamReader.close();
                bufferedReader.close();
                jsonData = stringBuilder.toString();
                Log.i("TAG", stringBuilder.toString());

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (jsonData != ""){
                getDtaFromJsonData(jsonData);//进一步分析 字符串 获取 json 里面数据
            } else {
                Log.i(TAG,"resultString is null");
            }

        }

    private void getDtaFromJsonData(String jsonData) {
        try {
            JSONObject jsonObject =   new JSONObject(jsonData);  //整体文件是一个对象
            String num = jsonObject.getString("num");    //根据键值对寻找数据
            String progectName = jsonObject.getString("progectName");
            Log.i(TAG,"num: " + num + "\n"
                           + "progectName: " + progectName
            );

            JSONObject dataObject = jsonObject.getJSONObject("data"); //data Json 对象
            String year = dataObject.getString("year");
            Log.i(TAG,"year: " + year);
            JSONArray jsonArrayWeek = dataObject.getJSONArray("week"); //week 是一个json 数组
            for (int i = 0; i < jsonArrayWeek.length(); i++) {
                String week = jsonArrayWeek.get(i).toString();
                Log.i(TAG,"week:" + week );
            }

            String booleanValue = dataObject.getString("boolean");
            Log.i(TAG,"booleanValue: " + booleanValue);

            JSONObject personObject = dataObject.getJSONObject("person");  //person 也是一个 json 对象
            String personName = personObject.getString("name");
            String personAge = personObject.getString("age");
            String personHobby = personObject.getString("hobby");

            Log.i(TAG, "personName: " + personName + "\n"
                        + "personAge" + personAge + "\n"
                        + "personHobby :" + personHobby);

            JSONArray animalArry = jsonObject.getJSONArray("animal"); //是一个JSon数组
            for (int i = 0; i < animalArry.length(); i++) {
                JSONObject jsonObjectAnimal = animalArry.getJSONObject(i); //获取 json  数据 里json  对象那个
                String animalName = jsonObjectAnimal.getString("name");
                String animalAge = jsonObjectAnimal.getString("age");

                Log.i(TAG, "animalname; " + animalName + "\n"
                          + "animalAge: " + animalAge + "\n");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
5、log输出

Android  解析本地Json数据_Data_02


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月25日 0

暂无评论

推荐阅读
HvTJUzsxOBtS