JSON有额外的引号和斜杠

时间:2018-05-09 16:54:12

标签: android json firebase firebase-cloud-messaging

编辑:这不是上述问题的重复,因为主要问题不是额外的斜杠 - 它是使用单个replaceAll无法删除的额外引号。 / p>

我使用以下代码将我的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;
}

2 个答案:

答案 0 :(得分:1)

当调用from os.path import isdir from os.path import dirname target = "P:\\2018\\Archive\\CO_007_II.zip" if not isdir(dirname(target)): print('cannot create zipfile because target does not exists') else: # create the zipfile 时,它会转换所有自动匹配的类型,对于不自动匹配的类型(如String而不是嵌套的JsonObject),它将引发异常。所以你需要告诉它如何使用自定义适配器从该String转换为自定义类型。

现在你应该能够自己做另一个。

jsonAdapter.fromJson(jsonString)

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

    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。你在问题中的例子甚至没有使用它。

答案 1 :(得分:0)

我从未使用过Moshi,但从我看到的情况来看,它将partner_informationrequest_information序列化为String而不是{{1} }}。检查您的父模型配置以查看注释是否正确。