繁体   English   中英

从资产中解析JSON数据

[英]Parsing JSON data from my assets

我正在尝试从资产中解析JSON数据,但是我的代码不断显示错误。 我想我做错了。

chem_elements.json

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

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;
    }

}

我在这方面是个新手,我想了解解析JSON数据的工作原理,请有人启发我吗? 提前谢谢。

错误堆栈

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.transactNative(本机方法),位于android.os.BinderProxy.transact(Binder.java:503)。 java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)上com.android.internal.os.RuntimeInit $ UncaughtHandler.uncaughtException(RuntimeInit.java:90)上的ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4425) .ThreadGroup.uncaughtException(ThreadGroup.java:690)

09-30 00:01:15.163 21598-21598 / com.example.ed.parsejson E / AndroidRuntime:致命异常:主进程: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字节)而失败在com.example.ed的org.json.JSONObject。(JSONObject.java:156)在org.json.JSONObject。(JSONObject.java:173)在org.json.JSONObject.nextValue(JSONTokener.java:100)在org.json.JSONObject。位于com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54)处的parsejson.MainActivity.parseJSONData(MainActivity.java:51)位于com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54)处在com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54)在com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54)在com.example.ed.parsejson.MainActivity.parseJSONData (MainActivity.java:54)在com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54)在com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54)在com.example .ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54)位于com.example.ed.parsejson.MainActivity.parseJSONData(MainActivity.java:54)

您正在内部调用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未初始化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM