简体   繁体   中英

Convert string with nested characters into specific json like format in java

There's a string like:

String query = "param1, param2, param3{npam1, npam2, npam3{nipam1, nipam2}}";

This string needs to be processed in the format:

{

    param1: param1, 
    param2: param2, 
    param3: {

        npam1: param3.npam1, 
        npam2: param3.npam2, 
        npam3: {

            nipam1: param3.npam3.nipam1, 
            nipam2: param3.npam3.nipam2

        }

    }

}

Have already done till 2 nested, but the point is the string to query can be extended to any number of nested curls.

What I did was to iterate over the objects in the string and then iterate over the attributes of each object. Hopefully, it will help you to solve your problem. Also in your initial String, you are missing the open parenthesis and the close parenthesis so I added them.

    String jsonWithOutFormat = "param1, param2, param3{npam1, npam2, npam3{nipam1, nipam2}}";
    jsonWithOutFormat = jsonWithOutFormat.replaceAll(" ", "");
    String json = "";
    String[] objectsInString = jsonWithOutFormat.split("[{]");
    List<String> nestedObjects = new ArrayList<>();
    json += "{";
    for (int i = 0; i < objectsInString.length; i++) {
        String[] objectAttributes = objectsInString[i].split("[,]");
        if(i==0)
            nestedObjects.add(objectAttributes[objectAttributes.length-1] + ".");
        else
            nestedObjects.add(nestedObjects.get(i-1)+objectAttributes[objectAttributes.length-1] + ".");
        for (int j = 0; j < objectAttributes.length; j++) {
            if(!(j == objectAttributes.length-1)) {
                if(i != 0)
                    json+=  objectAttributes[j] + ": " +  nestedObjects.get(i-1) + objectAttributes[j]  + ", ";
                else
                    json+=  objectAttributes[j] + "\"" + ": " + "\"" + objectAttributes[j] + "\"" + ", ";
            }
            else {
                if(!(i == objectsInString.length-1))
                    json+=  objectAttributes[j] + ": {";
                else {
                    json+= objectAttributes[j].replaceAll("}", "")  + ": " + nestedObjects.get(i-1) + objectAttributes[j];
                }
            }
        }
    }
    json += "}";
    System.out.print("\n" + json);
}

Thanks Jorge. This method can be called to produce json in desired format, by passing the actual String (the one that is not formatted).

public String expressionConstruct(String jsonWithOutFormat) {

    jsonWithOutFormat = jsonWithOutFormat.replaceAll(" ", "");
    String json = "";
    String[] objectsInString = jsonWithOutFormat.split("[{]");
    List<String> nestedObjects = new ArrayList<>();
    json += "{";
    for (int i = 0; i < objectsInString.length; i++) {
        String[] objectAttributes = objectsInString[i].split("[,]");
        if(i==0)
            nestedObjects.add(objectAttributes[objectAttributes.length-1] + ".");
        else
            nestedObjects.add(nestedObjects.get(i-1)+objectAttributes[objectAttributes.length-1] + ".");
        for (int j = 0; j < objectAttributes.length; j++) {
            if(!(j == objectAttributes.length-1)) {
                if(i != 0)
                    json+=  objectAttributes[j].replaceAll("}", "") + ": " +  nestedObjects.get(i-1) + objectAttributes[j]  + ", ";
                else
                    json+=  objectAttributes[j] + ": " + objectAttributes[j] + ", ";
            }
            else {
                if(!(i == objectsInString.length-1))
                    json+=  objectAttributes[j] + ": {";
                else {
                    json+= objectAttributes[j].replaceAll("}", "")  + ": " + nestedObjects.get(i-1) + objectAttributes[j];
                }
            }
        }
    }
    json += "}";
    return json;

}

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