2015-10-21 38 views
-3

我的android应用程序必须解析几种类型的json文本,例如下面的例子。Java(Android) - Json解析器的最佳类示例是什么?

{ 
    "text": "Hello World !", 
    "site": "http://helloworld.com" 
} 

所以我想做一个类来管理这个应用程序必须解析的每个json文本。像一个“JsonUtil.java”

我做了这样的课。

public class JsonUtil { 

    public static final String TAG = JsonUtil.class.getSimpleName(); 

    public JsonUtil() { 
    } 

    public NotificationAd parseNotificationAd(JSONObject jsonObject) { 
     NotificationAd notificationAd = new NotificationAd(); 
     try { 
      notificationAd.message = jsonObject.getString("text"); 
      notificationAd.targetUrl = jsonObject.getString("site"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     Log.i(TAG, "parseNotificationAd, notificationAd: " + notificationAd.toString()); 
     return notificationAd; 
    } 

    public class NotificationAd { 
     public String message; 
     public String targetUrl; 

     @Override 
     public String toString() { 
      return String.format("message: %s, targetUrl: %s", message, targetUrl); 
     } 
    } 

} 

我使用嵌套类太多“VO.java”类可以刺激总包结构的原因(我不知道为什么,只是我的口味:P太多的课让我复杂。)

所以用法是这样的,

JsonUtil.NotificationAd notificationAd = new JsonUtil().parseNotificationAd(response); 
String message = notificationAd.message; 
String targetUrl = notificationAd.targetUrl; 

我想知道如果我是正确的,其实我想要做班级为“抽象”,并使法“静态”像下面。

public abstract class JsonUtil { 

    public static final String TAG = JsonUtil.class.getSimpleName(); 

    public static NotificationAd parseNotificationAd(JSONObject jsonObject) { 
     NotificationAd notificationAd = new NotificationAd(); 
     try { 
      notificationAd.message = jsonObject.getString("text"); 
      notificationAd.targetUrl = jsonObject.getString("site"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     Log.i(TAG, "parseNotificationAd, notificationAd: " + notificationAd.toString()); 
     return notificationAd; 
    } 

    public static class NotificationAd { 
     public String message; 
     public String targetUrl; 

     @Override 
     public String toString() { 
      return String.format("message: %s, targetUrl: %s", message, targetUrl); 
     } 
    } 

} 

但我认为下一个代码有一些内存问题(这一点是我需要一些帮助,我不是专业的JAVA)。

任何人都可以建议哪些是在Android的Json解析器的最佳做法?

(我知道改装库,但PLZ别提了这样一个问题:P)

非常感谢每一个答案!

+1

你可以使用GSON序列化和反序列化JSON。 – motiver

+0

你可以试试Gson或Jackson。 –

+0

@motiver vipul_asri你可以给我一些示例代码吗? – wonsuc

回答

0
+0

我使用的是来自Google的Volley库的“JsonObjectRequest”,该类返回“org.json.JSONObject”用于http响应。是否需要更改此JSONObject到gson或杰克逊? – wonsuc

+0

安卓自己的JSON解析功能在android 11之前不在apis中,所以GSON&Jackson被广泛使用。我认为这可能是你需要的:http://www.technotalkative.com/android-json-parsing/ –

+0

我认为我的应用程序的min sdk是11以上,所以如果没关系? – wonsuc

0

结帐这个简单而容易的教程Getting Started with GSON

可以包括使用摇篮在项目中GSON依赖性:

compile 'com.google.code.gson:gson:2.3.1' 
+0

谢谢我阅读所有的引用url。你能读我的评论吗? [\t 我正在使用来自Google的Volley库的“JsonObjectRequest”。这个类为http响应返回“org.json.JSONObject”。我是否需要将此JSONObject更改为gson或jackson? ] – wonsuc

+0

不,你不必使用JSONObject或任何东西。只需通过上面的链接,您就可以轻松理解。 –

0

只需使用Gson给您的样本数据。这种方法显示了两种不同的方式来获得进入json使用Gson无论是StringJSONObject

public class Main { 

    public static void main(String[] args) { 
     new Main().gsonExample(); 
    } 

    /* Sample data 
    { 
    "text": "Hello World !", 
    "site": "http://helloworld.com" 
    } 
    */ 
    private void gsonExample() { 
     // sample data without the pretty printing 
     String jsonText = "{\"text\": \"Hello World !\",\"site\": \"http://helloworld.com\"}"; 

     // manually convert 
     JsonObject jsonObject = new JsonObject(); 
     jsonObject.addProperty("text", "Hello World !"); 
     jsonObject.addProperty("site", "http://helloworld.com"); 

     // assert the generated json is equal to the sample data 
     assert jsonObject.toString().equals(jsonText); 

     Model modelFromJsonObject = new Gson().fromJson(jsonObject.toString(), Model.class); 
     Model model = new Gson().fromJson(jsonText, Model.class); 
     // assert the two models are equals 
     assert modelFromJsonObject.equals(model); 

     // assert the text is set correctly 
     assert "Hello World !".equals(model.text); 

     System.out.println("All tests passed!"); 
    } 

    public static class Model { 
     public String text; 
     public String site; 
    } 
}