I don't have this in a public repository, so pardon the large post.
This cursor handles JSONObjects of two types: Page and Item. A Page
holds some page information and a JSONArray of Items and Item is a
single item.

---

package com.eyebrowsoftware.gobeering;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.database.AbstractCursor;
import android.os.Bundle;
import android.util.Log;

public class JSONObjectCursor extends AbstractCursor {
        private static final String TAG = "JSONObjectCursor";
        JSONObject mPage = null;
        JSONArray mArray = null;
        JSONObject mObject = null;
        String[] mProjection;
        String[] mKeys;
        HashMap<String, String> mProjectionMap;

        public JSONObjectCursor(String[] projection, JSONObject jso, String[]
keys,
                                                HashMap<String, String> 
projection_map) throws JSONException {
                mProjectionMap = projection_map;
                mProjection = projection;
                mKeys = keys;
                if(keys != null && keys.length == 2 && keys[0] != null & 
keys[1] !=
null) { // paged list type
                        // This combination of nulls and non-nulls
                        mPage = 
jso.getJSONObject(GoBeeringProvider.COLLECTION_KEY);
                        mArray = mPage.getJSONArray(keys[0]);
                } else if(keys != null && keys.length == 1 && keys[0] != null) {
                        // Another combination of nulls and non-nulls
                mObject = jso.getJSONObject(keys[0]);
                } else {
                        Log.e(TAG, "Internal Error - Illegal arguement to 
JSONObjectCursor:
keys[]");
                        throw new IllegalArgumentException();
                }
        }

        @Override
        public Bundle getExtras() {
                if(mPage != null) {
                        try {
                                Bundle b = new Bundle();
                        b.putInt(Pages.Page.PAGE, 
mPage.getInt(Pages.Page.PAGE));
                        b.putInt(Pages.Page.TOTAL_PAGES, mPage.getInt
(Pages.Page.TOTAL_PAGES));
                        b.putInt(Pages.Page.TOTAL_ENTRIES, mPage.getInt
(Pages.Page.TOTAL_ENTRIES));
                        b.putInt(Pages.Page.PER_PAGE, 30);
                        return b;
                        } catch (JSONException e) {
                                e.printStackTrace();
                                return Bundle.EMPTY;
                        }
                } else
                        return Bundle.EMPTY;
        }

        @Override
        public String[] getColumnNames() {
                return mProjection;
        }

        @Override
        public int getCount() {
                return (mArray != null) ? mArray.length() : 1;
        }

        private JSONObject getArrayItem() throws JSONException {
                JSONObject item_wrapper = 
mArray.getJSONObject(this.getPosition());
                return item_wrapper.getJSONObject(mKeys[1]);
        }

        @Override
        public double getDouble(int column) {
                try {
                        String key = mProjectionMap.get(mProjection[column]);
                        return (mArray != null) ? getArrayItem().getDouble(key) 
:
mObject.getDouble(key);
                } catch (JSONException e) {
                        e.printStackTrace();
                        return 0.0;
                }
        }

        @Override
        public float getFloat(int column) {
                try {
                        String key = mProjectionMap.get(mProjection[column]);
                        JSONObject o = (mArray != null) ? getArrayItem() : 
mObject;
                        return (o.isNull(key)) ? 0 : 
Float.valueOf(o.getString(key));
                } catch (JSONException e) {
                        e.printStackTrace();
                        return 0;
                }
        }

        @Override
        public int getInt(int column) {
                try {
                        String key = mProjectionMap.get(mProjection[column]);
                        JSONObject o = (mArray != null) ? getArrayItem() : 
mObject;
                        return o.getInt(key);
                } catch (JSONException e) {
                        e.printStackTrace();
                        return 0;
                }
        }

        @Override
        public long getLong(int column) {
                try {
                        String key = mProjectionMap.get(mProjection[column]);
                        JSONObject o = (mArray != null) ? getArrayItem() : 
mObject;
                        return o.getLong(key);
                } catch (JSONException e) {
                        e.printStackTrace();
                        return 0;
                }
        }

        @Override
        public short getShort(int column) {
                try {
                        String key = mProjectionMap.get(mProjection[column]);
                        JSONObject o = (mArray != null) ? getArrayItem() : 
mObject;
                        return (o.isNull(key)) ? 0 : 
Short.valueOf(o.getString(key));
                } catch (JSONException e) {
                        e.printStackTrace();
                        return 0;
                }
        }

        private static final String empty = new String();

        @Override
        public String getString(int column) {
                try {
                        String key = mProjectionMap.get(mProjection[column]);
                        JSONObject o = (mArray != null) ? getArrayItem() : 
mObject;
                        return (o.isNull(key)) ? empty : o.getString(key);
                } catch (JSONException e) {
                        e.printStackTrace();
                        return empty;
                }
        }

        @Override
        public boolean isNull(int column) {
                try {
                        String key = mProjectionMap.get(mProjection[column]);
                        JSONObject o = (mArray != null) ? getArrayItem() : 
mObject;
                        return o.isNull(key);
                } catch (JSONException e) {
                        e.printStackTrace();
                        return true;
                }
        }

}


---

On Jan 18, 3:11 am, Bert <[email protected]> wrote:
> You are always welcome to post.
>
> Thanks in advance
>
> On Jan 15, 7:34 pm, Brion Emde <[email protected]> wrote:
>
>
>
> > A JSONObject basically is a bean. It's a pretty sophisticated Map. I
> > do similar stuff to you, but I don't fool with Beans.
>
> > I encapsulated my web service as a content provider. It communicates
> > to me with JSONObjects, and JSONObjects packaging JSONArrays. I get
> > theJSONstring and parse it in the ContentProvider, then return a
> > custom Cursor-derived object that holds onto the JSONObject, and its
> > subcomponents, and does all the Cursor operations. So I never need to
> > copy any data, like what you're doing with your beans in the example
> > above.
>
> > I looked at the code and am not sure it would be that helpful to you.
> > If you want, I'll post it.
>
> > On Jan 15, 3:19 am, Bert <[email protected]> wrote:
>
> > > I'm using HttpClient to get the response of a webservice (REST
> > > servlet) which is aJSONstring.
> > > This string represents a JSONArray of type Map with key "mybeans".
> > > I'd like to assemble my bean using theJSONstring.
> > > Usually I use net.sf.jsonjson-lib and do something like this:
>
> > > String s = jsonStringIGotFromResponse;
> > > Map<String, List> bean = (Map<String, List>) JSONObject.toBean(
> > >                                 JSONObject.fromObject(s), Map.class);
> > > List myBeans = bean.get("mybeans");
> > > JSONArray jsonArray = JSONArray.fromObject(myBeans);
> > > List<MyBean> myBeansToReturn = new ArrayList<MyBean>();
> > > for (int i = 0; i < jsonArray.size(); i++) {
> > >         JSONObject jsonObject = jsonArray.getJSONObject(i);
> > >         MyBean myBean = (MyBean)JSONObject.toBean(jsonObject,
> > >                         MyBean.class);
> > >         myBeansToReturn.add(myBean);
>
> > > }
>
> > > I have no idea how to write something similar using the org.json
> > > classes provided by the Android.jar
>
> > > Could anyone help me out with this please?
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to