简体   繁体   English

多个JSON解析

[英]Multiple JSON parsing

I need help with making a decision on parsing. 我需要有关解析的决定方面的帮助。 I need to do JSON parsing. 我需要进行JSON解析。 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. 我这样做的方式是使用Volley进行一次通用样式的网络调用,并使用1个大型开关盒进行相关分析。 I would add a String to the method called source so I knew what parsing needs to be done. 我将一个String添加到名为source的方法中,因此我知道需要进行哪些解析。

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 for android。 Gson is a library that can easily parse JSON. Gson是一个可以轻松解析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. 使用Gson您可以创建模型类,所有解析都将为您完成。

Take a look at this tutorial: 看一下本教程:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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