简体   繁体   中英

how to convert wrong format of string into JSONObject

I am getting wrong format of json string from response. 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:

{
        "_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. 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:

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?

edit

based on @libanbn suggestion I tried with following program:

    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, ...} .

Before you try to convert the string to JSON object, you must change the format. Since the string follows a consistent pattern, you can use regular expressions to reformat it before parsing to JSON.

You could try this.. The idea is to build a valid json from your string.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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