如何以正确的方式将对象转换为GSON(JSON)?

时间:2016-03-22 21:22:11

标签: android json microsoft-band gson

我将bandClient对象转换为JSON时遇到问题(我想保存对象以供以后在SHARED PREFS(ANDROID)中使用)。

我尝试使用GSON,但在执行期间

String json = gson.toJson(bandClient);

我总是得到以下异常:

流程:xxr.com.mitracker,PID:19040                                                                                                  java.lang.StackOverflowError的                                                                                                        在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:380)                                                                                                        在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:375)                                                                                                        在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:380)                                                                                                        在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:375)                                                                                                        在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:380)

我怎样才能以正确的方式解决?

非常感谢任何建议

方法代码:

private AdapterView.OnItemClickListener onListItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            BandInfo[] pairedBands =
                    BandClientManager.getInstance().getPairedBands();

            Logger.d("Clicked on item " + position);
            final BandClient bandClient =
                    BandClientManager.getInstance().create(context, pairedBands[position]);
            //mBandList.get(position).getBandClient();

            MsBandHelperAssync asyncTask = new MsBandHelperAssync(new AsyncResponse() {
                @Override
                public void processFinish(Boolean output) {
                    try {
                        Logger.d("RETURNED " + output);
                        Gson gson = new Gson();
                        Logger.d("PLAIN OBJECT" + bandClient);
                        gson.toJsonTree(bandClient);
                        Logger.d("PASSED");
                        String json = gson.toJson(bandClient);
                        Logger.d("PASSED JSON" + json);
                        //Prefs.putString(Constants.Global.SHARED_PREFS_KEY_PAIRED_DEVICES, json);
                        Logger.d("Saved");
                        //CommonHelper.redirectToActivity(context, mActivity, DashboardActivity.class);
                    } catch (Exception e) {
                        Logger.e(e.getMessage());
                    }

                }
            });
            asyncTask.execute(bandClient);

1 个答案:

答案 0 :(得分:1)

BandClient表示与Micrsoft Health持有的BandService的连接。因此,它包含对用于创建它的活动,服务或应用程序上下文的引用。如果您将其保存以供以后使用,它将无法工作。

因此,您可能希望保存与要保存的BandClient关联的BandInfo对象的mac地址,然后在想要使用时重新创建BandClient。