简体   繁体   中英

Multiple JSON parsing

I need help with making a decision on parsing. I need to do JSON parsing. The way i was doing it was with one generic style network call using Volley and 1 massive switch case that would go to the relevant parse. I would add a String to the method called source so I knew what parsing needs to be done.

Initial call

private void getHomeScreen(){
   networkCall(
            url,         //Url used to make the call
            homeScreen);  //The source of the parsing
}

The response once the network call has completed sending it to the parser

onResponse{
   parser(
         jsonFile, //Response JSON file
         source);  //passing the String that had the source of the parser
}

The Parser method that sends the correct response to the correct parser

private void parser(JSONObject, String source){
 // the one massive switch case sending the HomeScreen to the relevant parser
}

Is there a better way to do this? there must be!

Thanks

If I were you I would look at Gson for android. Gson is a library that can easily parse JSON. So there is no need to make your own parsers. With Gson you can make model classes and all the parsing will be done for you.

Take a look at this tutorial:

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM