0

i am getting Response in JSON i want to parse this

    [
    {
        "fbuid": "100000462110782"
    },
    {
        "fullname": "Arun Pathania"
    },
    {
        "fbuid": "100000257902867"
    },
    {
        "fullname": "Shiju vargheae"
    },
    {
        "fbuid": "100003337246078"
    },
    {
        "fullname": "Smart Buzz"
    }
]

I tried the following:

JSONArray jArray = new JSONArray(result); 
for (int i=0; i < jArray.length(); i++) { 
    JSONObject oneObject = jArray.getJSONObject(i); // Pulling items from the array 
    u_id_json = oneObject.getString("fbuid");
    u_name_json = oneObject.getString("fullname");
}

Error

 error is coming is :--org.json.JSONException: No value for fullname –

please tell me how to parse this in android

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
Arun
  • 307
  • 1
  • 6
  • 12
  • 2
    easy but what have you tried? – Lazy Ninja May 07 '13 at 07:02
  • there are hundreds of examples on stackoverflow, did you try searching first? – ılǝ May 07 '13 at 07:03
  • http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android http://stackoverflow.com/questions/9715856/parsing-this-json – ılǝ May 07 '13 at 07:05
  • i am using this code :- JSONArray jArray = new JSONArray(result); for (int i=0; i < jArray.length(); i++) { JSONObject oneObject = jArray.getJSONObject(i); // Pulling items from the array u_id_json = oneObject.getString("fbuid"); u_name_json = oneObject.getString("fullname"); } and error is coming is :--org.json.JSONException: No value for fullname – Arun May 07 '13 at 07:11
  • 1
    Only every other of your objects has "fullname". The other half has "fbuid". It *is* a weird array you have there... – Thilo May 07 '13 at 07:13

1 Answers1

2
JSONArray main;
    int i;
    JSONObject mainobj,fb,fname;
    String[] fbuid,fullname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            try {
                main = new JSONArray(jsonstring);
                Log.e(main.toString(),"string");
                fbuid=new String[main.length()];
                fullname=new String[main.length()];
                for( i=0;i<main.length();i++)
                {
                    mainobj = main.getJSONObject(i);
                    if(mainobj.has("fbuid"))
                    {
                        fbuid[i]=mainobj.getString("fbuid");
                        Log.e(fbuid[i],"string");
                    }
                    else if(mainobj.has("fullname"))
                    {
                        fullname[i]=mainobj.getString("fullname");
                        Log.e(fullname[i],"string");
                    }
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

try this

ishu
  • 1,322
  • 1
  • 11
  • 15
  • Thanks Very much .. its working fine but at end giving java.lang.ArrayIndexOutOfBoundsException any idea? – Arun May 07 '13 at 07:46
  • in your for loop u use <= condition so it giving you error. – ishu May 07 '13 at 07:54
  • No i had'nt used <= condition exception is coming when i am printing in logs fullname[i]+""+ fbuid[i] outside for loop ...i have to get values from these String Array? – Arun May 07 '13 at 08:20