1

I want to add Two java JSON String manually , so for this i need to remove "}" and replace it with comma "," of first JSON String and remove the first "{" of the second JSON String . This is my program

import java.util.Map;
import org.codehaus.jackson.type.TypeReference;
public class Hi {
    private static JsonHelper jsonHelper = JsonHelper.getInstance();
    public static void main(String[] args) throws Exception {

        Map<String, Tracker> allCusts = null;

String A = "{\"user5\":{\"Iden\":4,\"Num\":1},\"user2\":{\"Iden\":5,\"Num\":1}}";

String B = "{\"user1\":{\"Iden\":4,\"Num\":1},\"user3\":{\"Iden\":6,\"Num\":1},\"user2\":{\"Iden\":5,\"Num\":1}}";

        String totalString = A + B;
        if (null != totalString) {
            allCusts = (Map<String, Tracker>) jsonHelper.toObject(
                    totalString, new TypeReference<Map<String, Tracker>>() {
                    });

        }
        System.out.println(allCusts);
    }
}

When adding two Strings A + B

I want to remove the last character of "}" in A and replace it with "," and remove the FIrst character of "{" in B .

SO this should it look like .

String A = "{\"user5\":{\"Iden\":4,\"Num\":1},\"user2\":{\"Iden\":5,\"Num\":1},";

String B = "\"user1\":{\"Iden\":4,\"Num\":1},\"user3\":{\"Iden\":6,\"Num\":1},\"user2\":{\"Iden\":5,\"Num\":1}}";

I have tried

String  Astr = A.replace(A.substring(A.length()-1), ",");
String  Bstr = B.replaceFirst("{", "");

String totalString =  Astr + Bstr ;

With this i was getting

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition

please suggest .

1
  • 2
    I would use a proper JSON deserializer and serializer like json.org/java. Commented Nov 5, 2012 at 4:38

3 Answers 3

5

{ is a control character for Regular Expressions, and since replaceFirst takes a string representation of a Regular Expression as its first argument, you need to escape the { so it's not treated as a control character:

String  Bstr = B.replaceFirst("\\{", "");

I would say that using the replace methods is really overkill here since you're just trying to chop a character off of either end of a string. This should work just as well:

String totalString = A.substring(0, A.length()-1) + "," + B.substring(1);
Sign up to request clarification or add additional context in comments.

Comments

1

Of course, regex doesn't look like a very good tool for this. But the following seem to work:

String str = "{..{...}..}}";
str = str.replaceFirst("\\{", "");
str = str.replaceFirst("}$", ","); 
System.out.println(str);

Output:

..{...}..},

3 Comments

I think str.replaceFirst("}$", ","); works just fine for the 2nd replace, and it has 4 fewer backslashes.
@DaoWen: Thanks. Added it to the answer, and that sure does look better. :)
Java really needs some sort of raw string syntax you can use for regular expressions like they have in Scala and C#. I wrote a regex a little while back that needed five backslashes in a row to get the escaping right in Java. Bleh.
0

Some issues in your first two statements. Add 0 as start index in substring method and leave with that. Put \\ as escape char in matching pattern and ut a , in second statement as replacement value.

String  Astr = A.substring(0, A.length()-1);//truncate the ending `}`
String  Bstr = B.replaceFirst("\\{", ",");//replaces first '{` with a ','
String totalString =  Astr + Bstr ;

Please note: There are better ways, but I am just trying to correct your statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.