JSON解析在从资产中读取时返回空值

时间:2018-02-14 13:11:05

标签: android json

我有一个JSON,如下所示:

{
  "questions": [
    {
      "id": "1",
      "question": "lorem ipsum dolor _______ amet",
      "optionA": "lorem",
      "optionB": "ipsum",
      "optionC" : "dolor",
      "optionD": "sit",
      "rightAnswer" : "sit"

    },
    {
      "id": "2",
      "question": "_____ ipsum dolor sit amet",
      "optionA": "lorem",
      "optionB": "ipsum",
      "optionC" : "dolor",
      "optionD": "sit",
      "rightAnswer" : "lorem"

    },
    {
      "id": "3",
      "question": "lorem _____ dolor sit amet",
      "optionA": "lorem",
      "optionB": "ipsum",
      "optionC" : "dolor",
      "optionD": "sit",
      "rightAnswer" : "ipsum"
    }
  ]
}  

json存储在Assets文件夹中。现在我想解析该JSON并将其插入到DB中的表中。这是我的代码:

package com.emc.kulkaa.learner;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

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

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

import static android.content.ContentValues.TAG;

/**
 * Created by kulkaa on 2/13/2018.
 */

public class ParseAction {
    private SQLiteDatabase SQLITEDATABASE;
    private Context context;

    String id, question, optionA, optionB, optionC, optionD, rightAnswer;

    ParseAction(Context context, SQLiteDatabase SQLITEDATABASE) {
        this.context = context;
        this.SQLITEDATABASE = SQLITEDATABASE;
        putData(id, question, optionA, optionB, optionC, optionD, rightAnswer);

        /*fetchData();*/
    }

    private String loadJSONFromAsset() {
        String json;
        try {
            InputStream inputStream = context.getAssets().open("questions.json1");
            //Log.d(TAG, "loadJSONFromAsset (inputstream): " + Arrays.toString(new InputStream[]{inputStream}));
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            // Log.d(TAG, "loadJSONFromAsset: " + input);
            inputStream.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return json;
    }

    private void putData(String id, String question, String optionA, String optionB, String optionC, String optionD, String rightAnswer) {
        try {
            JSONObject obj = new JSONObject(loadJSONFromAsset());
            JSONArray jsonArray = obj.getJSONArray("questions");

            ContentValues contentValues = new ContentValues();

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                String a = jsonObject.getString(id);
                String b = jsonObject.getString(question);
                String c = jsonObject.getString(optionA);
                String d = jsonObject.getString(optionB);
                String e = jsonObject.getString(optionC);
                String f = jsonObject.getString(optionD);
                String g = jsonObject.getString(rightAnswer);

                SQLITEDATABASE.execSQL(String.format("INSERT INTO quesTable " +
                        "( id, question, option1, option2, option3, option4, " +
                        "rightAnswer ) VALUES ('" + a + "','" + b + "','" + c + "','" + d + "', '" + e + "','" + f + "', '" + g + "')"));

                Log.d(TAG, "putData: " + jsonObject.getString("id"));
                Log.d(TAG, "putData: " + jsonObject.getString("question"));
                Log.d(TAG, "putData: " + jsonObject.getString("optionA"));
                Log.d(TAG, "putData: " + jsonObject.getString("optionB"));
                Log.d(TAG, "putData: " + jsonObject.getString("optionC"));

            }


        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }
}  

这将返回如下所示的异常:

02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err: org.json.JSONException: No value for null
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at com.emc.kulkaa.learner.ParseAction.putData(ParseAction.java:63)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at com.emc.kulkaa.learner.ParseAction.<init>(ParseAction.java:30)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at com.emc.kulkaa.learner.QuizActivity.onCreate(QuizActivity.java:40)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at android.app.Activity.performCreate(Activity.java:6679)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
02-14 18:37:04.701 15598-15598/com.emc.kulkaa.learner W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
02-14 18:37:04.702 15598-15598/com.emc.kulkaa.learner W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-14 18:37:04.702 15598-15598/com.emc.kulkaa.learner W/System.err:     at android.os.Looper.loop(Looper.java:154)
02-14 18:37:04.702 15598-15598/com.emc.kulkaa.learner W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
02-14 18:37:04.702 15598-15598/com.emc.kulkaa.learner W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
02-14 18:37:04.702 15598-15598/com.emc.kulkaa.learner W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
02-14 18:37:04.702 15598-15598/com.emc.kulkaa.learner W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  

如何从JSON获取值?

0 个答案:

没有答案