0

I have an android application which gets a json string.

[
{
    "apellidos": "seguro",
    "edad": 2,
    "idninos": 1,
    "nombre": "antonio",
    "padres": "n1"
},
{
    "apellidos": "martinez",
    "edad": 2,
    "idninos": 2,
    "nombre": "sebastian",
    "padres": "n2"
},
{
    "apellidos": "lopez",
    "edad": 2,
    "idninos": 3,
    "nombre": "francisaca",
    "padres": "n3"
},
{
    "apellidos": "maroto",
    "edad": 2,
    "idninos": 4,
    "nombre": "gregoria",
    "padres": "n4"
},
{
    "apellidos": "maroto",
    "edad": 2,
    "idninos": 5,
    "nombre": "javier",
    "padres": "n4"
},
{
    "apellidos": "ramirez",
    "edad": 1,
    "idninos": 6,
    "nombre": "andrea",
    "padres": "n5"
},
{
    "apellidos": "leal",
    "edad": 1,
    "idninos": 7,
    "nombre": "jorge",
    "padres": "n6"
},
{
    "apellidos": "rodriguez",
    "edad": 2,
    "idninos": 8,
    "nombre": "antonio",
    "padres": "n7"
},
{
    "apellidos": "gomez",
    "edad": 2,
    "idninos": 9,
    "nombre": "jaime",
    "padres": "n8"
},
{
    "apellidos": "jimenez",
    "edad": 2,
    "idninos": 10,
    "nombre": "nerea",
    "padres": "n9"
},
{
    "apellidos": "gutierrez",
    "edad": 2,
    "idninos": 11,
    "nombre": "paquito",
    "padres": "n12"
},
{
    "apellidos": "esteso",
    "edad": 2,
    "idninos": 12,
    "nombre": "tomas",
    "padres": "n15"
}
]

And I show it in this way:I show in a listview;

public class MainActivity extends ListActivity {
private static final String LOGTAG = "LogsAndroid";  
static String id;
   EditText eTetx1;
//contexto
private Context context;
//url
private static String    

   url="http://192.168.1.33:8080/WSBaby1/webresources/com.gleegoo.ws.ninos/";
//variable de json
private static final String TAG_NOMBRE="nombre";
private static final String TAG_APELLIDO="apellidos";
//lista arrays desplegables
ArrayList<HashMap<String,String>> jsonlist= new ArrayList<HashMap<String,String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //llamando la actividad
    new GetJSONActivity(MainActivity.this).execute();
     eTetx1 = (EditText)  findViewById(R.id.editText1);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
private class GetJSONActivity extends AsyncTask<String, Void, String>{
    private ListActivity activity;


    public GetJSONActivity(ListActivity activity){
        this.activity=activity;
        context = activity;
    }

    @Override
    protected String doInBackground(String... params) {

        JSONParser jParser = new JSONParser();
        //getting JSON string from url
        JSONArray json = jParser.GetJSONfromUrl(url);
        for (int i = 0; i < json.length(); i++) {
            try {
                JSONObject c = json.getJSONObject(i);
                String vpeli=c.getString(TAG_NOMBRE);
                String apell=c.getString(TAG_APELLIDO);

                HashMap<String, String> map = new HashMap<String, String>();
                //
                map.put(TAG_NOMBRE, vpeli);
                map.put(TAG_APELLIDO, apell);
                jsonlist.add(map);

            } catch (JSONException e) {
                // TODO: handle exception
                e.printStackTrace();
                return "Error creando variables";
            }
        }
        return "EXITO";
    }

   @Override
   protected void onPostExecute(String success){
       ListAdapter adapter = new SimpleAdapter(context, jsonlist,R.layout.items, new String[] {TAG_NOMBRE,TAG_APELLIDO}, new int[]{R.id.nombre,R.id.tipo});
    setListAdapter(adapter);
    //selecting single listview
    getListView();
   }

Well, this works perfectly, my problem is this: If I want to show, for example all data from an id, idnino

eg 3.{"apellidos":"lopez","edad":2,"idninos":3,"nombre":"francisaca","padres":"n3"}

He had thought about changing the url and put this:

private static String url="http://192.168.1.33:8080/WSBaby1/webresources/com.gleegoo.ws.ninos/3

But I get it but can not show it .. This is my logcat:

  04-01 12:43:51.165: E/JSON Parser(17792): Error traduciendo    
datosorg.json.JSONException: Value  
{"nombre":"francisaca","padres":"n3","idninos":3,"apellidos":"lopez","edad":2} of type   
 org.json.JSONObject cannot be converted to JSONArray
 04-01 12:43:51.175: W/dalvikvm(17792): threadid=12: thread exiting with uncaught    
exception (group=0x419612a0)
04-01 12:43:51.240: E/AndroidRuntime(17792): FATAL EXCEPTION: AsyncTask #1
04-01 12:43:51.240: E/AndroidRuntime(17792): java.lang.RuntimeException: An error  
occured while executing doInBackground()
04-01 12:43:51.240: E/AndroidRuntime(17792):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at 
java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at         
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at  
java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at 
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at  
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at 
java.lang.Thread.run(Thread.java:856)
04-01 12:43:51.240: E/AndroidRuntime(17792): Caused by: java.lang.NullPointerException
04-01 12:43:51.240: E/AndroidRuntime(17792):    at 
com.example.jsonprueba.MainActivity$GetJSONActivity.doInBackground(MainActivity.java:65)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at 
com.example.jsonprueba.MainActivity$GetJSONActivity.doInBackground(MainActivity.java:1)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at 
android.os.AsyncTask$2.call(AsyncTask.java:287)
04-01 12:43:51.240: E/AndroidRuntime(17792):    at 
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-01 12:43:51.240: E/AndroidRuntime(17792):    ... 5 more
Layke
  • 51,422
  • 11
  • 85
  • 111
Emmanuelguther
  • 1,039
  • 4
  • 19
  • 29
  • @anuruddhika Please don't embolden arbitrary words in your edits. – Duncan Jones Apr 01 '14 at 10:55
  • U can reference from this [post](http://stackoverflow.com/questions/12312102/how-to-json-parsing-in-android) – Spinning Apr 01 '14 at 10:59
  • you are trying to parse JSONObject to JSONArray. {"apellidos":"lopez","edad":2,"idninos":3,"nombre":"francisaca","padres":"n3"} this is JSONObject not JSONArray. – Yuvaraja Apr 01 '14 at 11:07

3 Answers3

2

This is a JSONArray and not a JSONObject - to make a JSONObject from it, use

JSONObject jsonObject = jsonArray.getJSONObject(0);

this gets the first JSONObject from this JSONArray.

If you have multiple JSONObjects, use this:

JSONObject jsonObject;
for(int n = 0; n < jsonArray.length(); n++)
{
    jsonObject = jsonArray.getJSONObject(n);
}

To get the values:

jsonObject.getString("name");
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • thanks for your reply but I can not make it work. Can you help me modify the method doInBackground? – Emmanuelguther Apr 01 '14 at 11:23
  • dude it is soooo simple just take one for loop which u already took then add this JSONObject jsonObject = jsonArray.getJSONObject(0); in to the for loop pass the value of int loop in the getJSONObject(int value) then inside of every this use my jsonObject loop just try it dude – Bhanu Sharma Apr 01 '14 at 11:33
2

You are trying to parse the JSON as an Array

JSONArray json = jParser.GetJSONfromUrl(url);

but your JSON is an object

{
    "apellidos": "lopez",
    "edad": 2,
    "idninos": 3,
    "nombre": "francisaca",
    "padres": "n3"
}

your JSON should be an array with one object only

[{
    "apellidos": "lopez",
    "edad": 2,
    "idninos": 3,
    "nombre": "francisaca",
    "padres": "n3"
}]

you can try to check if the result object is an array or an object like this

Object jsonResultObject = jParser.GetJSONfromUrl(url);

if (jsonResultObject!=null) {
    if (jsonResultObject instanceof JSONArray) {
        ......
    }else if (jsonResultObject instanceof JSONObject) {
        .....
    }else{
        throw new Exception("The result object is not a JSON object or an Array");
    }

}
vzamanillo
  • 9,905
  • 1
  • 36
  • 56
1

Remove array from your coding and do it with object as the JSON you showed is of Objects and not of arrays.

Arrays starts with [ ] Objects with { }

Your JSON starts with array and element inside it are in arrays.

Saggy
  • 624
  • 8
  • 23