0

I need to parse this JSON:

{
  "out":{
    "nroRegistros":1,
    "asignaciones":[
      {
        "lat":"456",
        "lng":"456",
        "direccion":"Nocedal 108    Estacion Central",
        "depto":null,
        "descripcion":"Casa amplia, cerca del metro las rejas",
        "tipoVehiculo":null,
        "referencia":null,
        "rutDenunciante":null,
        "nombreDenunciante":null,
        "apePaternoDenunciante":null,
        "apeMaternoDenunciante":null,
        "fonoMovilDenunciante":null,
        "ambito":null,
        "prioridad":null,
        "ley":null,
        "articulo":null,
        "marcaVehiculo":null,
        "colorVehiculo":null,
        "placaPatente":null,
        "id":null
      }
    ]
  },
  "status":{
    "code":1,
    "message":"success"
  }
}

From all I have read I cant find an example or something to guide me. I am new to json and i can't really find a way to make it work. I have read a lot of tutorials but they all are quite simple. I understand them but I cant make this one work.

user2672165
  • 2,986
  • 19
  • 27
Shocklo
  • 467
  • 2
  • 17

3 Answers3

0

First of all, use some json parser to visualize data, for example http://jsonviewer.stack.hu/. This will make it for you a lot easier to understand the structure of the data.

Next step is to create a model class to accept the json you are receiving. This you have to do it by yourself, in eclipse or any other IDE you might be using.

It will look something like this:

public class JsonModel{
     public Object out;
     public Object status;
}

here I put Object as a general type, you may want to define the variables to an appropriate type to reflect the structure of the json file.

Once you have the model, you can simply get the data by any json library, I like to use Gson 3rd party for any json work. It would be something like so:

string json = getJsonFromInternet(); 
JsonModel mymodel = new Gson().fromJson(json, JsonModel.class);

your data will be stored in mymodel, as Java object, which you can use according to your needs.

I hope this helps.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • May I ask why you remove the accepted from my answer ? – NiVeR Apr 03 '14 at 18:18
  • lag and pressed it two times, helped me a lot, and after a lot of reading another problem that i had was this : if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } xP, add it and presto everything worked, thanks for your help and soffy for removing the accepted xD – Shocklo Apr 04 '14 at 07:45
0

find Json parsing tutorials

Json Parsing good example

User10001
  • 1,295
  • 2
  • 18
  • 30
0
You can achieve this by using JsonLib.

Try to put those values in HashMap , make sure you create the same structure of pojo classes as defined in the Json String (case sensitive)

Your json will be mapped using the classMap.put method. From there on , we have a great controller over the java bean object.

Try to explore few things, before you jump into it

String json = "{'out':[{'test':'testname'},{'test2':'testname2'}]}";  
Map classMap = new HashMap();  
classMap.put( "out", YourClass.class );  
MyBean bean = JSONObject.toBean( JSONObject.fromObject(json), MyBean.class, classMap ); 


Reference

<link>http://json-lib.sourceforge.net/snippets.html</link>
Sireesh Yarlagadda
  • 12,978
  • 3
  • 74
  • 76