我正在尝试在我的 android APP 中解析一些 JSON。但是我遇到了两个错误。第一个是“字符串类型的值不能转换为 JSON 对象”(这个错误我已经通过更改几行代码来消除,但可能会搞砸一些东西,但我还不知道导致应用程序正在运行)。第二个错误是空指针错误。
我知道这里已经有很多关于这个的代码,因为他们都使用 hive 教程直接跳转到一个数组中。我已经尝试编辑此代码以将我的 JSON 放入 ListView 中,但无法完全理解它。我认为我的问题是我在解析器中返回了一个 JSONObject,它调用了我的第一个对象(已识别),然后我的代码立即跳转到数组中,因为已识别不是数组,所以没有数组,信息是。那么如何在代码中获取Identified的对象,然后获取信息的对象(从而用上面的代码跳转到信息数组中)呢?我从来没有做过 JSON 解析,所以这对我来说有点困难。我很确定这是问题所在,我只是不知道如何解决它。这让我疯狂。我在 JSON/Android 上可以找到的所有内容都使用以数组开头的 JSON,我的代码将负责处理。我只是不知道如何执行上述操作:获取第二个对象的数组。任何帮助将不胜感激。几天来我一直在绞尽脑汁尝试不同的事情。该应用程序有时会运行并且不会在屏幕上显示任何内容,有时应用程序会崩溃并显示 String 无法转换 JSONObject 的错误,并且有几次我遇到空指针错误。就像我提到的那样,我认为这是因为当我应该在“信息”上调用它时,我试图在“已识别”上调用该数组。但我不知道如何通过 Identified 然后跳入信息数组。我确信它只是在正确的地方写了几行代码,但我不知道在哪里。我只是不知道如何执行上述操作:获取第二个对象的数组。任何帮助将不胜感激。几天来我一直在绞尽脑汁尝试不同的事情。该应用程序有时会运行并且不会在屏幕上显示任何内容,有时应用程序会崩溃并显示 String 无法转换 JSONObject 的错误,并且有几次我遇到空指针错误。就像我提到的那样,我认为这是因为当我应该在“信息”上调用它时,我试图在“已识别”上调用该数组。但我不知道如何通过 Identified 然后跳入信息数组。我确信它只是在正确的地方写了几行代码,但我不知道在哪里。我只是不知道如何执行上述操作:获取第二个对象的数组。任何帮助将不胜感激。几天来我一直在绞尽脑汁尝试不同的事情。该应用程序有时会运行并且不会在屏幕上显示任何内容,有时应用程序会崩溃并显示 String 无法转换 JSONObject 的错误,并且有几次我遇到空指针错误。就像我提到的那样,我认为这是因为当我应该在“信息”上调用它时,我试图在“已识别”上调用该数组。但我不知道如何通过 Identified 然后跳入信息数组。我确信它只是在正确的地方写了几行代码,但我不知道在哪里。任何帮助将不胜感激。几天来我一直在绞尽脑汁尝试不同的事情。该应用程序有时会运行并且不会在屏幕上显示任何内容,有时应用程序会崩溃并显示 String 无法转换 JSONObject 的错误,并且有几次我遇到空指针错误。就像我提到的那样,我认为这是因为当我应该在“信息”上调用它时,我试图在“已识别”上调用该数组。但我不知道如何通过 Identified 然后跳入信息数组。我确信它只是在正确的地方写了几行代码,但我不知道在哪里。任何帮助将不胜感激。几天来我一直在绞尽脑汁尝试不同的事情。该应用程序有时会运行并且不会在屏幕上显示任何内容,有时应用程序会崩溃并显示 String 无法转换 JSONObject 的错误,并且有几次我遇到空指针错误。就像我提到的那样,我认为这是因为当我应该在“信息”上调用它时,我试图在“已识别”上调用该数组。但我不知道如何通过 Identified 然后跳入信息数组。我确信它只是在正确的地方写了几行代码,但我不知道在哪里。其他时候,应用程序只是崩溃并显示 String 无法转换 JSONObject 的错误,并且有几次我得到空指针错误。就像我提到的那样,我认为这是因为当我应该在“信息”上调用它时,我试图在“已识别”上调用该数组。但我不知道如何通过 Identified 然后跳入信息数组。我确信它只是在正确的地方写了几行代码,但我不知道在哪里。其他时候,应用程序只是崩溃并显示 String 无法转换 JSONObject 的错误,并且有几次我得到空指针错误。就像我提到的那样,我认为这是因为当我应该在“信息”上调用它时,我试图在“已识别”上调用该数组。但我不知道如何通过 Identified 然后跳入信息数组。我确信它只是在正确的地方写了几行代码,但我不知道在哪里。
JSON如下:
{
"identified": "yes",
"Information":
[
{
"state": "Arkansas",
"type": "landlocked",
"additionalinfo": "{ search: 'online', website: 'http://states.arkansas.com', latitudeandlongitude: '93905, 63550}",
"outdoorfun": "yes",
"cold": "no"
},
{
"state": "chicago",
"type": "windy",
"additionalinfo": "{ search: 'online', website: 'http://states.chicago.com', latitudeandlongitude: '23905, 45355}",
"outdoorfun": "no",
"cold": "yes"
}
]
}
Activity类代码的主要部分如下:
// JSON Node names
private static final String TAG_IDENTIFIED = "id";
private static final String TAG_INFORMATION = "information";
private static final String TAG_STATE = "state";
private static final String TAG_TYPE = "type";
private static final String TAG_ADDITIONALINFO = "additionalinfo";
private static final String TAG_OUTDOORFUN = "outdoorfun";
private static final String TAG_COLD = "cold";
// contacts JSONArray
JSONArray information = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> stateList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try{
for(int i = 0; i < information.length(); i++){
information =json.getJSONArray(TAG_INFORMATION);
JSONObject c = information.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_STATE);
String type = c.getString(TAG_TYPE);
// Phone number is agin JSON Object
JSONObject additionalinfo = c.getJSONObject(ADDITIONALINFO);
String outdoorfun = c.getString(TAG_OUTDOORFUN);
String cold = c.getString(TAG_COLD);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_TYPE, type);
map.put(TAG_OUTDOORFUN, outdoorfun);
informationList.add(map);
}
}
catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, informationList,
R.layout.list_item,
new String[] {TAG_NAME, TAG_TYPE, TAG_OUTDOORFUN }, new int[] {
R.id.name, R.id.type, R.id.outdoorfun });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String type = ((TextView) view.findViewById(R.id.type)).getText().toString();
String outdoorfun = ((TextView) view.findViewById(R.id.outdoorfun)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_TYPE, type);
in.putExtra(TAG_OUTDOORFUN, outdoorfun);
startActivity(in);
}
});
解析器在这里:
herepackage com.androidhive.jsonparsing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class JSONParser extends Activity {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
单列表活动在这里:
package com.androidhive.jsonparsing;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SingleMenuItemActivity extends Activity {
// JSON node keys
private static final String TAG_ID = "id";
private static final String TAG_INFORMATION = "information";
private static final String TAG_STATE = "state";
private static final String TAG_TYPE = "type";
private static final String TAG_ADDITIONALINFO = "additionalinfo";
private static final String TAG_OUTDOORFUN = "outdoorfun";
private static final String TAG_COLD = "cold";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra(TAG_NAME);
String type = in.getStringExtra(TAG_TYPE);
String outdoorfun = in.getStringExtra(TAG_OUTDOORFUN);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblType = (TextView) findViewById(R.id.type_label);
TextView lblOutdoorfun = (TextView) findViewById(R.id.outdoorfun_label);
lblName.setText(name);
lblType.setText(type);
lblOutdoorfun.setText(outdoorfun);
}
}