I am trying to parse JSON data from my assets, but my codes keep showing up error. I think I am doing something wrong.
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;
}
}
I am very new at this and I am trying to understand how does parsing JSON data works, please could anybody enlighten me on this matter ? Thx in advance.
Error Stack
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 android.os.TransactionTooLargeException: data parcel size 1251724 bytes at android.os.BinderProxy.transactNative(Native Method) at android.os.BinderProxy.transact(Binder.java:503) at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4425) at com.android.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)