JSON 有额外的引号和斜杠



编辑:这不是上述问题的重复,因为主要问题不是额外的斜杠 - 而是额外的引号,无法通过单个替换全部删除。

我使用以下代码将我的 FCM 响应转换为 JSON 格式:

public void onMessageReceived(RemoteMessage remoteMessage)
{
try
{
Map<String, String> params = remoteMessage.getData();
if(params != null)
{
JSONObject jsonObject = new JSONObject(params);
Object notificationObject = parseJson(jsonObject.toString());
if(notificationObject instanceof ClientRequestAcceptedModel)
{
Log.d(TAG, ((ClientRequestAcceptedModel) notificationObject).getFeedback());
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

我得到以下信息:

{
"notification_type": "request_accepted",
"partner_information": "{"zip":"24000","country":"canada","address":"any raw address","city":"some-city","device_meta":{"device_id":"av0384yuhyiush23768","device_type":"android"},"last_name":"Ahmed1","created_at":"2018-04-04 16:28:59","avatar":"some image path","partner":1,"password_hash":"yasir123","last_modefied":"2018-04-04 16:28:59","phone_number":"+921234567890","location":{"latitude":"1234567","longitude":"1234567"},"id":2,"first_name":"Yasir1","email":"yasirahmed15@yopmail.com","customer":1,"status":1}",
"feedback": "request accepted",
"request_information": "{"request_type":"custom","request_quotation":true,"created_at":"2018-05-07 15:57:13","media":[{"body":"base64string","type":"image"},{"body":"base64string","type":"video"},{"body":"base64string","type":"audio"}],"schedule_date":"0000-00-00","client_id":2,"pStatus":0,"partner_id":2,"schedule_time_from":"00:00:00","updated_at":"0000-00-00 00:00:00","schedule_time_to":"00:00:00","skill":{"name":"Pipe Fitting","id":"1"},"extra_notes":"some extra notes","request_location":{"address":"Some raw address of the client if any","latitude":"1234567","longitude":"1234567"},"id":23,"status":0}"
}

如您所见,有大量额外的\(斜杠(和一些额外的"(引号(。我必须使用以下方法将其转换为有效的JSON:

JSONObject jsonObject = new JSONObject(params);
String modifier = jsonObject.toString().replaceAll("\\", "");
String modifier2 = modifier.replace(""{"", "{"");
String modifier3 = modifier2.replace("}"", "}");

有什么方法可以正确地做到这一点吗?我不能查看所有 JSON 并寻找要替换/修复的内容。

编辑:这是对象

public class ClientRequestAcceptedModel
{
@Json(name = "feedback") private String feedback;
@Json(name = "partner_information") private UserModel partnerInformation;
@Json(name = "request_information") private RequestInformationModel requestInformation;
}
public class RequestInformationModel
{
@Json(name = "id") private String id;
@Json(name = "client_id") private String clientId;
@Json(name = "partner_id") private String partnerId;
@Json(name = "skill_id") private String skillId;
@Json(name = "latitude") private String latitude;
@Json(name = "longitude") private String longitude;
@Json(name = "address") private String address;
@Json(name = "request_type") private String requestType;
@Json(name = "request_quotation") private Boolean requestQuotation;
@Json(name = "extra_notes") private String extraNotes;
@Json(name = "status") private String status;
@Json(name = "created_at") private String createdAt;
@Json(name = "updated_at") private String updatedAt;
@Json(name = "pStatus") private String pStatus;
}

编辑:添加下面用户要求的模型

public class RequestInformationModelAdapter extends JsonAdapter<RequestInformationModel>
{
@Override
public RequestInformationModel fromJson(JsonReader reader) throws IOException
{
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<RequestInformationModel> jsonAdapter = moshi.adapter(RequestInformationModel.class);
return jsonAdapter.fromJson(reader.nextString());
}
@Override
public void toJson(JsonWriter writer, RequestInformationModel value) throws IOException
{
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<RequestInformationModel> jsonAdapter = moshi.adapter(RequestInformationModel.class);
writer.value(jsonAdapter.toJson(value));
}
}

调用jsonAdapter.fromJson(jsonString)时,它会转换所有自动匹配的类型,对于不自动匹配的类型(如字符串而不是嵌套的 JsonObject(,它将引发异常。因此,您需要告诉它如何使用自定义适配器从该字符串转换为自定义类型。

现在你应该可以自己做另一个了。

Moshi moshi = new Moshi.Builder().add(RequestInformationModel.class, new RequestInformationModelAdapter()).build();
JsonAdapter<ClientRequestAcceptedModel> jsonAdapter = moshi.adapter(ClientRequestAcceptedModel.class);
ClientRequestAcceptedModel clientRequestAccepted;
try {
clientRequestAccepted = jsonAdapter.fromJson(json);
System.out.println(clientRequestAccepted);
} catch (IOException e) {
e.printStackTrace();
}

下面的此类负责将嵌套的 json 转换为 RequestInformationModel。您必须再次告诉 moshi 将此字符串转换为哪个类。

public class RequestInformationModelAdapter extends JsonAdapter<RequestInformationModel> {
@Override
public RequestInformationModel fromJson(JsonReader reader) throws IOException {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<RequestInformationModel> jsonAdapter = moshi.adapter(RequestInformationModel.class);
return jsonAdapter.fromJson(reader.nextString());
}
@Override
public void toJson(JsonWriter writer, RequestInformationModel value) throws IOException {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<RequestInformationModel> jsonAdapter = moshi.adapter(RequestInformationModel.class);
writer.value(jsonAdapter.toJson(value));
}
}

你真的应该先尝试使用Moshi。您在问题中的示例甚至没有使用它。

我从未使用过 Moshi,但据我所知,它将partner_informationrequest_information序列化为String而不是JSONbject。检查父模型配置以查看注释是否正确。

相关内容

  • 没有找到相关文章

最新更新