使用GSON将条目Map到JSON的转换未传递请求中的所有条目

osh3o9ms  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(176)

我正在尝试使用GSON将JSON请求数据转换为JSON字符串。为此我使用了map。但是我可以看到,请求中的一些map条目被忽略了。

String jsonStrReq = null;
Map<String, String> patient1 = new HashMap<String, String>();
        patient1.put("diagnosis", diagnosis);
        patient1.put("doctorId", Prefs.getString("docid", ""));
        patient1.put("patientId", null);
        patient1.put("patientName", name);
        patient1.put("patientUhid", uhid);
        patient1.put("mobileNumber", phone);
        patient1.put("age", age);
        patient1.put("gender", gender);
        patient1.put("area", area);
        patient1.put("city", city);
        patient1.put("tagColor", tagColor);
        patient1.put("treatments", treatment);
        patient1.put("profilePic", null);
        Gson gsonReq = new GsonBuilder().serializeNulls().create();
        jsonStrReq = gsonReq.toJson(patient1);
        Log.d("Testing", "Add Patient passed json  "+jsonStrReq);

我正在打印的请求字符串为:

Add Patient passed json  {"area":"fhghf","patientName":"chfnb","gender":"Female","city":"gjfhhf","tagColor":"Red","doctorId":"32","patientUhid":"cbfjcg","mobileNumber":"xxxxxxxx","age":"25"}

如果你看到一些条目完全丢失。只是想知道我在这里做错了什么。请帮助

czq61nw1

czq61nw11#

我尝试了一下你的代码(只是用字符串值替换了你的变量作为你的Map)
我有所有的钥匙。所以不知道你怎么了

{"area":"area","patientName":"name","gender":"gender","patientId":null,"city":"city","tagColor":"tagColor","patientUhid":"uhid","mobileNumber":"phone","profilePic":null,"diagnosis":"diagnosis","treatments":"treatment","doctorId":"Prefs.getString","age":"age"}

String jsonStrReq = null;
        Map<String, String> patient1 = new HashMap<String, String>();
        patient1.put("diagnosis", "diagnosis");
        patient1.put("doctorId", "Prefs.getString");
        patient1.put("patientId", null);
        patient1.put("patientName", "name");
        patient1.put("patientUhid", "uhid");
        patient1.put("mobileNumber", "phone");
        patient1.put("age", "age");
        patient1.put("gender", "gender");
        patient1.put("area", "area");
        patient1.put("city", "city");
        patient1.put("tagColor", "tagColor");
        patient1.put("treatments", "treatment");
        patient1.put("profilePic", null);
        Gson gsonReq = new GsonBuilder().serializeNulls().create();
        jsonStrReq = gsonReq.toJson(patient1);
        DebugLog.send(jsonStrReq);

相关问题