我有点困惑这个问题,我即将解析JSON到我的Android应用程序。我用php创建了我自己的模型:
class Users extends CI_Model
{
function get_all()
{
$query = $this->db->get('usrs');
foreach ($query->result() as $row)
{
//$name = $row->Username;
$data = $row;
echo json_encode($data);
}
}
}
我的控制器:
class Rest extends CI_Controller
{
public function index()
{
echo $this->load->model("Users");
echo "<br />";
echo $this->Users->get_all();
}
}
但是当我跑这个时它只是给了我这个:
{"UserAccessId":"1","Username":"Paul","Password":"Parreno","FirstName":"John Paul","MiddleName":"Pineda","LastName":"Parreno","Email":"johnpaul_sandwich_chicosci@yahoo.com"}
没有数组的名称。
这是我在Android应用程序中正在处理的代码:
private static String url = "http://localhost/kwotted/index.php/rest";
private static final String TAG_NAME = "Username";
private static final String TAG_FNAME = "FirstName";
private static final String TAG_LNAME = "LastName";
private static final String TAG_DB = "";
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
//contacts = json.getJSONArray(TAG_CONTACTS);
contacts = json.getJSONArray(TAG_DB);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
//String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String fname = c.getString(TAG_FNAME);
String lname = c.getString(TAG_LNAME);
//String email = c.getString(TAG_EMAIL);
//String address = c.getString(TAG_ADDRESS);
//String gender = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
/*JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
*/
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
//map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_FNAME, fname);
map.put(TAG_LNAME, lname);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_FNAME, TAG_LNAME }, new int[] {
//TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
R.id.name, R.id.email, R.id.mobile });
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 cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_FNAME, cost);
in.putExtra(TAG_LNAME, description);
startActivity(in);
}
});
}
但是android在行上抛出NullPointerException:
contacts = json.getJSONArray(TAG_DB);
答案 0 :(得分:0)
从这个JSON输出
{“UserAccessId”:“1”,“用户名”:“保罗”,“密码”:“Parreno”,“FirstName”:“John Paul”,“MiddleName”:“Pineda”,“LastName”:“Parreno”, “电子邮件”: “johnpaul_sandwich_chicosci@yahoo.com”}
它不是您正在处理的数组,而只是一个JSON对象。
也许您可以尝试以下方法:
JSONObject obj = (JSONObject) new JSONTokener(json_string_output).nextValue();
String name = obj .getString(TAG_NAME);
String fname = obj .getString(TAG_FNAME);
String lname = obj .getString(TAG_LNAME);
您可以参考JSONTokener API