我有点被这个问题困住了,我即将将 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);