我正在尝试从 url 解析一个 json 条目。下面是代码段。
TextView txtViewParsedValue;
private JSONObject jsonObject;
private static String url = "https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";
String strParsedValue = null;
TextView txtViewParsedValue;
private JSONObject jsonObject;
private static String url = "https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";
String strParsedValue = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtViewParsedValue = (TextView) findViewById(R.id.textview1);
try {
parseJSON();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void parseJSON() throws JSONException
{
JSONParser jParser = new JSONParser();
jsonObject = jParser.getJSONFromUrl(url);
JSONObject object = jsonObject.getJSONObject("student");
JSONArray subArray = object.getJS ONArray("student");
for(int i=0; i<subArray.length(); i++)
{
strParsedValue+="\n"+subArray.getJSONObject(i).getString("id").toString();
}
txtViewParsedValue.setText(strParsedValue);
}
我想提取学生的所有 id 并在 textview 中打印它们。它给出空指针异常。搜索了几个与此相关的帖子并尝试了它们,但没有奏效,任何指导都会有很大帮助!