有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java解析我的资产中的JSON数据

我试图从我的资产解析JSON数据,但我的代码总是显示错误。我想我做错了什么

化学元素。json

{
    "Hydrogen": {
        "symbol" : "H",
        "atomic_number" : 1,
        "atomic_weight" : 1.00974
    },
    "Helium" : {
        "symbol" : "He",
        "atomic_number" : 2,
        "atomic_weight" : 4.002602
    }
}

主要活动。爪哇

package com.example.ed.parsejson;

import 安卓.app.Activity;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.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;
    }

}

我对这一点非常陌生,我正试图理解解析JSON数据是如何工作的,请有人能告诉我这方面的情况吗? Thx提前

错误堆栈

09-30 00:01:15.256 21598-21598/? D/Error﹕ ERR: TOTAL BYTES WRITTEN: 1251640 09-30 00:01:15.257 21598-21598/? E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!! (parcel size = 1251724) 09-30 00:01:15.257 21598-21598/? E/AndroidRuntime﹕ Error reporting crash 安卓.os.TransactionTooLargeException: data parcel size 1251724 bytes at 安卓.os.BinderProxy.transactNative(Native Method) at 安卓.os.BinderProxy.transact(Binder.java:503) at 安卓.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4425) at com.安卓.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:90) at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

09-30 00:01:15.163 21598-21598/com.example.ed.parsejson E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.ed.parsejson, PID: 21598 java.lang.OutOfMemoryError: Failed to allocate a 53 byte allocation with 627648 free bytes and 612KB until OOM; failed due to fragmentation (required continguous free 4096 bytes for a new buffer where largest contiguous free 0 bytes) at org.json.JSONObject.(JSONObject.java:114) at org.json.JSONTokener.readObject(JSONTokener.java:350) at org.json.JSONTokener.nextValue(JSONTokener.java:100) at org.json.JSONObject.(JSONObject.java:156) at org.json.JSONObject.(JSONObject.java:173) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:51) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54) at com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54)


共 (1) 个答案

  1. # 1 楼答案

    您正在自身内部调用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");
    

    请删除该行,然后再试一次

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