1

I have following objects in JSON file. I have to parse it and store it in a file. What will be the android code for doing this?

 {
  "result":"ok",
  "numbers":
           [
            {
              "First":"first",
              "Second":"second",
              "Third":"third",
              "Fourth":"fourth",
              "Fifth":"fifth"
            }
           ]
 }

Any one find me getting out of this? I would really appreciate your work.

Pihu
  • 1,041
  • 11
  • 35
Anish Panthi
  • 797
  • 8
  • 17

4 Answers4

2
{ -> json object 
  "result":"ok", 
  "numbers":[-> json array 
            {

Do like this

JSONObject jobject=new JSONObject(jsonString);
JSONArray jarray=Jobject.getJSONArray("numbers");
String result=jobject.getJSONObject("result");
for(int i=0;jarray.length();i++){
      String first= jarray.getJSONObject(i).getString("First");
      String Second= jarray.getJSONObject(i).getString("Second");
}
Nambi
  • 11,944
  • 3
  • 37
  • 49
2
 { // json object node
  "result":"ok", 
  "numbers":[// json array numbers
            {
              "First":"first",

To parse

JSONObject jb = new JSONObject("your json");
String result = (JSONArray)jb.getString("result");
JSONArray jr = (JSONArray)jb.getJSONArray("numbers");
JSONObject jb1= (JSONObject) jr.getJSONObject(0); 
String first = jb1.getString("First");
// similarly for second third and fourth 

Once you parse you can write the result to a file.

Edit:

Note: Network operation must be done in a background thread. Use Asynctask

try
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet("your json url ");  
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
String _response=EntityUtils.toString(resEntity); 
}catch(Exception e)
{
      e.printStackTrace();
}

Now use _response JSONObject jb = new JSONObject("_response);. Rest all is the same

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Try Using the following

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

JSONObject json = null;
JSONArray jsonArray = null;
String data = null;

json = new JSONObject(response);
data = json.getString("numbers");
jsonArray = new JSONArray(data);

for (int i = 0; i < jsonArray.length(); i++) {
String str =jsonArray.getJSONObject(i).toString();
}
virtualpathum
  • 753
  • 2
  • 11
  • 23
0

always remember { means object and [ means array so you can proceed with following code in the give

JSONObject firstjobject=new JSONObject(jsonString);
JSONArray firstjarray=firstjobject.getJSONArray("numbers");
String result=firstjobject.getJSONObject("result");
for(int i=0;firstjarray.length();i++){
      String first= firstjarray.getJSONObject(i).getString("First");
      String Second= firstjarray.getJSONObject(i).getString("Second");
}

here numbers is an array and First,Second etc are the keys for relative data values

Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43