Android JSON有额外的引号和斜杠

Android JSON有额外的引号和斜杠,android,json,firebase,firebase-cloud-messaging,Android,Json,Firebase,Firebase Cloud Messaging,编辑:这不是上述问题的重复,因为主要问题不是额外的斜杠,而是无法用单个替换全部删除的额外引号 我使用以下代码将FCM响应转换为JSON格式: public void onMessageReceived(RemoteMessage remoteMessage) { try { Map<String, String> params = remoteMessage.getData(); if(params != null)

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

我使用以下代码将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();
    }
}
如您所见,有大量额外的\(斜杠)和一些额外的(引号)。我必须使用以下方法将其转换为有效的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));
    }
}
公共类RequestInformationModelAdapter扩展了JsonAdapter
{
@凌驾
来自JSON(JsonReader)的公共RequestInformationModel引发IOException
{
Moshi-Moshi=新的Moshi.Builder().build();
JsonAdapter=moshi.adapter(RequestInformationModel.class);
返回jsonAdapter.fromJson(reader.nextString());
}
@凌驾
public void toJson(JsonWriter writer,RequestInformationModel值)抛出IOException
{
Moshi-Moshi=新的Moshi.Builder().build();
JsonAdapter=moshi.adapter(RequestInformationModel.class);
writer.value(jsonAdapter.toJson(value));
}
}

我从未使用过Moshi,但据我所见,它将
合作伙伴信息
请求信息
序列化为
字符串
,而不是
JSONObject
。检查父模型配置以查看注释是否正确。

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();
    }
Moshi-Moshi=new Moshi.Builder().add(RequestInformationModel.class,new RequestInformationModelAdapter()).build();
JsonAdapter JsonAdapter=moshi.adapter(ClientRequestAcceptedModel.class);
clientrequesteceptedmodel clientrequestecepted;
试一试{
clientRequestAccepted=jsonAdapter.fromJson(json);
System.out.println(客户端接受);
}捕获(IOE异常){
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));
        }
    }
公共类RequestInformationModelAdapter扩展了JsonAdapter{
@凌驾
来自JSON(JsonReader)的公共RequestInformationModel引发IOException{
Moshi-Moshi=新的Moshi.Builder().build();
JsonAdapter=moshi.adapter(RequestInformationModel.class);
返回jsonAdapter.fromJson(reader.nextString());
}
@凌驾
public void toJson(JsonWriter writer,RequestInformationModel值)抛出IOException{
Moshi-Moshi=新的Moshi.Builder().build();
JsonAdapter=moshi.adapter(RequestInformationModel.class);
writer.value(jsonAdapter.toJson(value));
}
}

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

您是否尝试在logNo中打印
jsonObject
,我是通过调试器获得它的值的。@NileshRathod,这不是重复的。删除斜杠不是主要问题,而是引号。无论您的数据类是什么,它都是不正确的。或者不是后端正在为partner_信息和request_信息发送转义的json值。看起来您甚至没有使用Moshi。此外,您不应该使用这样的对象类,instanceof是一种代码味道。@AfzalivE添加了这些类。UserModel类很好(它已经在其他地方使用并且可以工作)。它们已正确注释。您可以共享您尝试序列化的实际父模型吗?添加了对象。我尝试了您提供的代码。不起作用。我收到了相同的“预期开始对象”,但路径$处有字符串。合作伙伴\u信息“问题发生在使用Moshi之前,这就是我没有添加代码的原因。我从jsonObject.toString()获得的“字符串”有额外的引号和斜杠。问题在于它们,而不是随后的解析;因为您必须对合作伙伴信息执行相同的操作(使用自定义适配器)。我';我已经说过了,您有一个格式错误的json,它不是';不能正确嵌套。要么在后端修复json,要么使用答案中提供的解决方案之一。这个错误清楚地说明了这一点,它需要一个json对象作为合作伙伴信息,而json只有一个字符串。非常感谢您的帮助。为另一个模型编写了一个类似的适配器,它成功了。标记您的答案是否正确。@Asim您可以共享您的适配器代码。
    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));
        }
    }