Java 无法在Android Studio中读取JSON

Java 无法在Android Studio中读取JSON,java,android,json,Java,Android,Json,我想在Android Studio中读取/解析JSON文件 资产文件夹中包含的JSON文件如下: { "Person": { "Name": "John" } } import ... public class ParseJ { private Context context; String name; public String RetName() throws JSONExce

我想在Android Studio中读取/解析JSON文件

资产文件夹中包含的JSON文件如下:

{
   "Person":
   {
   "Name": "John"
   }
}
import ...

public class ParseJ
{
    private Context context;
    String name;


    public String RetName() throws JSONException {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONObject student = obj.getJSONObject("Person");
        name = student.getString("Name");
        return name;
    }

    public String loadJSONFromAsset()
    {
        String json = null;
        try
        {
            InputStream is = context.getAssets().open("Test.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;
    }
}
应该读取文件的类是:

{
   "Person":
   {
   "Name": "John"
   }
}
import ...

public class ParseJ
{
    private Context context;
    String name;


    public String RetName() throws JSONException {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONObject student = obj.getJSONObject("Person");
        name = student.getString("Name");
        return name;
    }

    public String loadJSONFromAsset()
    {
        String json = null;
        try
        {
            InputStream is = context.getAssets().open("Test.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;
    }
}
然后在MainActivity中调用它,如下所示:

String firstname = new String();
ParseJ parsetest = new ParseJ();
TextView onomaTView;


 try {
        firstname = parsetest.RetName();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //textview
    onomaTView = (TextView) findViewById(R.id.onoma);

        onomaTView.setText("ONOMA:" + firstname);
    }                                  
```
当我构建并运行应用程序时,它会立即崩溃,并查看日志,它会显示以下消息: 原因:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“android.content.res.AssetManager android.content.Context.getAssets()”


这让我相信它无法读取文件,它只返回null。

您得到的是null指针,因为上下文现在为null 创建一个构造函数并像这样传递上下文

 public class ParseJ
    {
        private Context context;
        String name;
    
    public ParseJ(Context context){
    this.context = context
    }
然后在Mainactivity中创建ParseJ的对象

ParseJ parsetest = new ParseJ(this);

这回答了你的问题吗?试着这样做这确实会消除我一直遇到的错误,但现在我有了一个新的错误,它是由:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int java.lang.String.length()”引起的。你能告诉我at ehich行吗?这是一堆错误,但这里有一些:at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:121)在org.json.JSONTokener.nextValue(JSONTokener.java:98)在org.json.JSONObject.(JSONObject.java:164)在org.json.JSONObject.(JSONObject.java:181)你能添加Xml文件吗?@PuhskintilioNo我必须用json来做。