Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm kinda stuck with this problem, I'm about to parse JSON to my android app. I created my own model in php using this:

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);
    }
}
}

My controller:

 class Rest extends CI_Controller
 {
public function index()
{
    echo $this->load->model("Users");
        echo "<br />";
        echo $this->Users->get_all();
}

 }

But when I ran this it just gives me this:

 {"UserAccessId":"1","Username":"Paul","Password":"Parreno","FirstName":"John Paul","MiddleName":"Pineda","LastName":"Parreno","Email":"[email protected]"}

without the name of the array.

This is the code I'm working on in my android app:

   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);

            }
        });



    }

But android throws a NullPointerException at line:

    contacts = json.getJSONArray(TAG_DB);
share|improve this question
2  
I hope that's not real user data! :-) – Ken Wolf Jun 8 at 13:03
I don't know the name of the array so it's throwing NullPointerException – neknek mouh Jun 8 at 13:08
What you show as output from controller is valid JSON. JSON arrays won't have an array name prefixed to them. I think the problem is with your Java code. – hw. Jun 8 at 13:23
Hi JSON is set, so how do I know the name for the json array I don't know what it is called but the one who includes the curly braces (key-value pairs) – neknek mouh Jun 8 at 13:25

1 Answer

up vote 0 down vote accepted

From this JSON output

{"UserAccessId":"1","Username":"Paul","Password":"Parreno","FirstName":"John Paul","MiddleName":"Pineda","LastName":"Parreno","Email":"[email protected]"}

It is not an array you are dealing with but just one JSON object.
Perhaps you can try the following :

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);

you may refer to the JSONTokener API

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.