简体   繁体   English

如何将错误格式的字符串转换为 JSONObject

[英]how to convert wrong format of string into JSONObject

I am getting wrong format of json string from response.我从响应中得到了错误的json字符串格式。 Now I need to correct it.现在我需要纠正它。 But I am not able to do.但我做不到。 Below is the response I am getting from some mongodb related API:以下是我从一些与mongodb相关的 API 得到的响应:

{
        "_id" : ObjectId("5ecd66aa9fd30b21cac18beb"),
        "_class" : "com.wpits.mongo.docs.UssdMongoLog",
        "jsonObject" : {
                "message" : "MessageBase{CommandLength=148, CommandID=BasicContinue, CommandStatus=0, SenderCB=-516163402, ReceiverCB=1896231456, AccountName=atg189, Password=AtG189, SystemType=, InterfaceVersion=23, UssdVersion=PHASEIII, UssdOpType=Request, MsIsdn=211919002392, ServiceCode=152, CodeScheme=sevenDigit, UssdString=Reply with Song Id to Subscribe.\n870. Nixx Corona IVR English 02\n295. Nixx Tone\n\n #. Main Menu, SwitchMode=0, ChargeRatio=0, ChargeType=0, ChargeSource=null, ChargeLocation=0, Message=null}",
                "timestamp" : "2020-05-26 09:58:398"
        }
}

Now, as it can be seen key message has a value which is not a correct JSON format.现在,可以看出关键message的值不是正确的JSON格式。 I wanted this to be like below:我希望它如下所示:

"message" : "MessageBase" : {"CommandLength"="148", "CommandID"="BasicContinue", "CommandStatus"="0", "SenderCB"="-516163402", "ReceiverCB"="1896231456", "AccountName"="atg189", "Password"="AtG189", "SystemType"="", "InterfaceVersion"="23", "UssdVersion"="PHASEIII", "UssdOpType"="Request",....}

I am using org.json.simple.JSONObject and org.json.simple.JSONParser like below:我正在使用org.json.simple.JSONObjectorg.json.simple.JSONParser如下所示:

public static void main(String[] args) {
    try {
    JSONObject obj = new JSONObject();
    obj.put("message", "MessageBase{CommandLength=148, CommandID=BasicContinue, CommandStatus=0, SenderCB=-516163402, ReceiverCB=1896231456, AccountName=atg189, Password=AtG189, SystemType=, InterfaceVersion=23, UssdVersion=PHASEIII, UssdOpType=Request, MsIsdn=211919002392, ServiceCode=152, CodeScheme=sevenDigit, UssdString=Reply with Song Id to Subscribe.\n870. Nixx Corona IVR English 02\n295. Nixx Tone\n\n #. Main Menu, SwitchMode=0, ChargeRatio=0, ChargeType=0, ChargeSource=null, ChargeLocation=0, Message=null}");
    String msg = (String)obj.get("message");
    msg = msg.replace("MessageBase", "\"MessageBase\" : ");
    JSONParser parser = new JSONParser();
    JSONObject obj2 = (JSONObject)parser.parse(msg);
    System.out.println(obj2.toJSONString());
    }catch(Exception ex) {
        ex.printStackTrace();
    }
}

But not able to do that... is there any utility class that I can use in order to correct this format?但无法做到这一点......我可以使用任何实用程序 class 来纠正这种格式吗?

edit编辑

based on @libanbn suggestion I tried with following program:根据@libanbn 的建议,我尝试了以下程序:

    public static void main(String[] args) {
    try {
    JSONObject obj = new JSONObject();
    obj.put("message", "MessageBase{CommandLength=148, CommandID=BasicContinue, CommandStatus=0, SenderCB=-516163402, ReceiverCB=1896231456, AccountName=atg189, Password=AtG189, SystemType=, InterfaceVersion=23, UssdVersion=PHASEIII, UssdOpType=Request, MsIsdn=211919002392, ServiceCode=152, CodeScheme=sevenDigit, UssdString=Reply with Song Id to Subscribe.\n870. Nixx Corona IVR English 02\n295. Nixx Tone\n\n #. Main Menu, SwitchMode=0, ChargeRatio=0, ChargeType=0, ChargeSource=null, ChargeLocation=0, Message=null}");
    String msg = (String)obj.get("message");

    msg = msg.replace("MessageBase", "");
    msg = msg.replace("\n", "");
    msg = msg.replace("{", "{\"");
    msg = msg.replace("}", "\"}");
    msg = msg.replace("=", "\"=\"");
    msg = msg.replace(",", "\",\"");
    msg = "{\"message\" : "+msg+"}";
    msg = msg.replace("=", ":");
    System.out.println(msg);
    JSONParser parser = new JSONParser();
    JSONObject obj2 = (JSONObject)parser.parse(msg);
    System.out.println(obj2.toJSONString());
    }catch(Exception ex) {
        ex.printStackTrace();
    }
}

it solved my problem..它解决了我的问题..

You can see that the string follows a pattern of MessageBase{key=value, key=value, ...} .您可以看到字符串遵循MessageBase{key=value, key=value, ...}的模式。

Before you try to convert the string to JSON object, you must change the format.在尝试将字符串转换为 JSON object 之前,您必须更改格式。 Since the string follows a consistent pattern, you can use regular expressions to reformat it before parsing to JSON.由于字符串遵循一致的模式,您可以在解析为 JSON 之前使用正则表达式重新格式化它。

You could try this.. The idea is to build a valid json from your string.你可以试试这个。这个想法是从你的字符串中构建一个有效的 json 。

public static void main(String[] args) {
    try {
        JSONObject obj = new JSONObject();

        String msg = (String) obj.get("message");
        msg = "MessageBase{CommandLength=148, CommandID=BasicContinue, CommandStatus=0, SenderCB=-516163402, ReceiverCB=1896231456, AccountName=atg189, Password=AtG189, SystemType=, InterfaceVersion=23, UssdVersion=PHASEIII, UssdOpType=Request, MsIsdn=211919002392, ServiceCode=152, CodeScheme=sevenDigit, UssdString=Reply with Song Id to Subscribe.\n870. Nixx Corona IVR English 02\n295. Nixx Tone\n\n #. Main Menu, SwitchMode=0, ChargeRatio=0, ChargeType=0, ChargeSource=null, ChargeLocation=0, Message=null}";
        msg = msg.replace("MessageBase{", "{\"MessageBase\" : \"");
        msg = msg.substring(0, msg.length()-1).concat("\"}");
        JSONParser parser = new JSONParser();
        JSONObject obj2 = (JSONObject) parser.parse(msg);
        System.out.println(obj2.toJSONString());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM