如何在Android中调用JSON文件?

时间:2014-11-26 06:32:26

标签: android json

我想在Android Activity中读取json文件。但是它获得了NullPointerException。这是我的que_static.txt文件json数据。

 {
       "Questions": [
        {
          "Question": "class PassA 
             {
               public static void main(String [] args) 
                {
                  PassA p = new PassA();
                  p.start();
                }
              void start() 
              {
                  long [] a1 = {3,4,5};
                  long [] a2 = fix(a1);
                  System.out.print(a1[0] + a1[1] + a1[2] + " ");
                  System.out.println(a2[0] + a2[1] + a2[2]);
              }

              long [] fix(long [] a3) 
              {
                  a3[1] = 7;
                  return a3;
               }}",

          "CorrectAnswer":,
          "Answer":"",
          "Answers":[
                {
                 "Answer": "12 15"
                },
                {
                "Answer": "15 15"
                },
                {
                "Answer": "3 4 5 3 7 5"
                },
                {
                "Answer": "3 7 5 3 7 5"
                }
            ]
          }
       ]
    }

这是我的活动代码

private void loadQuestions() throws Exception
        {
            String strTopicName = textTopicName.getText().toString().trim();
            StringBuilder quesString = null;
            String aJsonLine = null ;
            InputStream questions = null;
            try 
            {
        if(strTopicName.equals("Static classes"))
            { 
           questions=this.getBaseContext().getResources().openRawResource(R.raw.que_static);
        }

         catch (Exception e)
            {

            } finally
            {
                try
                {
                    bReader.close();
                } 
                catch (Exception e)
                {
                    Log.e("", e.getMessage().toString(), e.getCause());
                }

            }
       }

        public static JSONArray getQuesList() 
        {
            return quesList;
        }
}

当我调用此文件时,它会获得nullpointerexception。

11-26 11:57:32.549: D/class com.quizfun.QuestionActivity(393): {  "Questions": [    {      "Question": "class PassA {    public static void main(String [] args)     {        PassA p = new PassA();        p.start();    }    void start()     {        long [] a1 = {3,4,5};        long [] a2 = fix(a1);        System.out.print(a1[0] + a1[1] + a1[2] + " ");        System.out.println(a2[0] + a2[1] + a2[2]);    }    long [] fix(long [] a3)     {        a3[1] = 7;        return a3;    }}",      "CorrectAnswer":,      "Answer":"",      "Answers":[         {            "Answer": "12 15"              },          {           "Answer": "15 15"           },          {           "Answer": "3 4 5 3 7 5"         },              {           "Answer": "3 7 5 3 7 5"         }        ]      }   ]}

11-26 11:57:32.549: D/AndroidRuntime(393): Shutting down VM
11-26 11:57:32.589: E/AndroidRuntime(393): FATAL EXCEPTION: main
11-26 11:57:32.589: E/AndroidRuntime(393): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quizfun/com.quizfun.QuestionActivity}: java.lang.NullPointerException
11-26 11:57:32.589: E/AndroidRuntime(393):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-26 11:57:32.589: E/AndroidRuntime(393):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-26 11:57:32.589: E/AndroidRuntime(393):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-26 11:57:32.589: E/AndroidRuntime(393):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-26 11:57:32.589: E/AndroidRuntime(393):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 11:57:32.589: E/AndroidRuntime(393):  at android.os.Looper.loop(Looper.java:123)
11-26 11:57:32.589: E/AndroidRuntime(393):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-26 11:57:32.589: E/AndroidRuntime(393):  at java.lang.reflect.Method.invokeNative(Native Method)
11-26 11:57:32.589: E/AndroidRuntime(393):  at java.lang.reflect.Method.invoke(Method.java:507)
11-26 11:57:32.589: E/AndroidRuntime(393):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-26 11:57:32.589: E/AndroidRuntime(393):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-26 11:57:32.589: E/AndroidRuntime(393):  at dalvik.system.NativeStart.main(Native Method)
11-26 11:57:32.589: E/AndroidRuntime(393): Caused by: java.lang.NullPointerException
11-26 11:57:32.589: E/AndroidRuntime(393):  at com.quizfun.QuestionActivity.onCreate(QuestionActivity.java:108)
11-26 11:57:32.589: E/AndroidRuntime(393):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-26 11:57:32.589: E/AndroidRuntime(393):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-26 11:57:32.589: E/AndroidRuntime(393):  ... 11 more

1 个答案:

答案 0 :(得分:2)

尝试从Assets文件夹中获取json数据文件,尝试使用它。

public String getJsonFromAssets() {
    String json = null;
    try {

        InputStream is = getAssets().open("YOURFILENAME.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        json = new String(buffer, "UTF-8");


    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}

//然后像下面那样使用json。

JSONObject object = new JSONObject(getJsonFromAssets());
JSONArray jsonArray = object.getJSONArray("Questions");
for (int i = 0; i < jsonArray.length(); i++){
     JSONObject jObject = jsonArray.getJSONObject(i);
     //now do parsing as per your need here.
}

}