从我的资产中解析JSON数据

Cha*_*ras 2 java android json

我试图解析我的资产中的JSON数据,但我的代码一直出现错误.我想我做错了什么.

chem_elements.json

{
    "Hydrogen": {
        "symbol" : "H",
        "atomic_number" : 1,
        "atomic_weight" : 1.00974
    },
    "Helium" : {
        "symbol" : "He",
        "atomic_number" : 2,
        "atomic_weight" : 4.002602
    }
}
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

package com.example.ed.parsejson;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

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

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;


public class MainActivity extends Activity {

    TextView text;

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

        text = (TextView) findViewById(R.id.text);

        parseJSONData();
    }

    public JSONObject parseJSONData() {
        String JSONString = null;
        JSONObject JSONObject = null;
        try {
            // Open the inputStream to the file
            InputStream inputStream = getAssets().open("chem_elements.json");

            int sizeOfJSONFile = inputStream.available();

            // array that will store all the data
            byte[] bytes = new byte[sizeOfJSONFile];

            // reading data into the array from the file
            inputStream.read(bytes);

            // close the input stream
            inputStream.close();

            JSONString = new String(bytes, "UTF-8");
            JSONObject = new JSONObject(JSONString);

            // Get the JSON Object from the data
            JSONObject parent = this.parseJSONData();

            // This will store all the values inside "Hydrogen" in an element string
            String element = parent.getString("Hydrogen");

            // This will store "1" inside atomicnumber
            String atomicNumber = parent.getJSONObject("Hydrogen").getString("atomic_number");


            text.setText("element : " + element + " atomicNumber : " + atomicNumber);

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (JSONException x) {
            x.printStackTrace();
            return null;
        }
        return JSONObject;
    }

}
Run Code Online (Sandbox Code Playgroud)

我是非常新的,我想了解解析JSON数据是如何工作的,请有人在此事上启发我吗?Thx提前.

错误堆栈

09-30 00:01:15.256 21598-21598 /?D /错误:错误:总计写:1251640 09-30 00:01:15.257 21598-21598 /?E/JavaBinder:!!! 失败的粘合剂交易!(包裹大小= 1251724)09-30 00:01:15.257 21598-21598 /?E/AndroidRuntime:错误报告崩溃android.os.TransactionTooLargeException:数据包大小1251724字节在Android.os.BinderProxy.transact(Binder.java:503)android.O的android.os.BinderProxy.transactNative(Native Method). ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4425)位于java.lang上java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)的com.android.internal.os.RuntimeInit $ UncaughtHandler.uncaughtException(RuntimeInit.java:90) .ThreadGroup.uncaughtException(ThreadGroup.java:690)

09-30 00:01:15.163 21598-21598/com.example.ed.parsejson E/AndroidRuntime:FATAL EXCEPTION:main进程:com.example.ed.parsejson,PID:21598 java.lang.OutOfMemoryError:无法分配53字节分配627648个空闲字节和612KB直到OOM; 因org.json.JSONTokener.readObject(JSONTokener.java:350)org.json.JSONObject.(JSONObject.java:114)中的碎片(需要最大连续空闲0字节的新缓冲区所需的连续空闲4096字节)而失败at org.json.JSONTokener.nextValue(JSONTokener.java:100)atg.json.JSONObject.(JSONObject.java:156)atg.json.JSONObject.(JSONObject.java:173)at com.example.ed. com.example.ed.parsejson.MainActivity中的parsejson.MainActivity.parseJSONData(MainActivity.java:51).

Hon*_*uan 5

你在parseJSONData()内部调用它会导致无限递归调用:

        JSONString = new String(bytes, "UTF-8");
        JSONObject = new JSONObject(JSONString);

        // Get the JSON Object from the data
        JSONObject parent = this.parseJSONData(); // <-- this line

        // This will store all the values inside "Hydrogen" in an element string
        String element = parent.getString("Hydrogen");
Run Code Online (Sandbox Code Playgroud)

只需删除该行,然后重试.

顺便说一句,你TextView text没有初始化.