1

I understand there are many stackoverflow question and answer can help to fix this. My Question is not about fixing but writing properly. I need people view and I make better decision.

Here is JSON example :

"SearchResult": {
    "usersList": [
        {
            "name": "userId",
            "value": "1009"
        },
        {
            "name": "userName",
            "value": "{userName}"
        },
        {
            "name": "userUrl",
            "value": "{userUrl}"
        }
    ]
}

We have test that overwrite this JSON and send as resultList to application.

For overwrite we have put value to HashMap:

public Map<String, String> createUserList(String userName, String userUrl) {
    Map<String, String> putsValue = new HashMap<String, String>();
    //putsValue.put("{userName}", userName);
    putsValue.put("\\{userName\\}", userName);
    putsValue.put("\\{userUrl\\}", userUrl);
    return putsValue;
}

Question is When I used

putsValue.put("{userName}", userName);

When I try to parse JSON using following code :

String jsonTemplate = "fileName.json";
JSONTokener tokener = new JSONTokener(new FileReader(jsonTemplate));
JSONObject root = new JSONObject(tokener);
parsedJson = root.toString();

for (Map.Entry<String, String> putsValue: putsValue.entrySet()) {
    parsedJson = parsedJson
                    .replaceAll(putsValue.getKey(), putsValue.getValue());
}

I am getting this error

java.util.regex.PatternSyntaxException: Illegal repetition
{userName}
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.closure(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)

How I can find tags like this:

putsValue.put("{userName}", userName);

And replace value I don't want to do this way

putsValue.put("\\{userName\\}", userName);
3
  • putsValue.put("{userName}", userName); has nothing to do with PatternSyntaxException. Not clear what you are asking about. Commented Aug 4, 2016 at 13:43
  • Yes right let me update Commented Aug 4, 2016 at 13:45
  • replaceAll -> read the documentation for that method. Commented Aug 4, 2016 at 13:59

3 Answers 3

1

You can try Pattern.quote(String) as next:

parsedJson = parsedJson
                .replaceAll(Pattern.quote(putsValue.getKey()), putsValue.getValue());
Sign up to request clarification or add additional context in comments.

2 Comments

I have more than 20 value. Is there way I can minimize this efforts because I don't think so if I use Pattern.quote for all put my code review pass.
I updated my answer, you can do it once when you call replaceAll instead
0

No need in custom json mapper in your case. Use Jackson. You need just to provide getters/setters and annotate fields.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonInclude;

Deserialize json:

ObjectMapper mapper = new ObjectMapper();
Clazz obj = mapper.readValue(jsonString, Clazz.class);

Serialize to json:

   class Clazz {
       @JsonInclude(JsonInclude.Include.NON_NULL)
       ... field declaration;
   }
   String json = mapper.writeValueAsString(obj);

Comments

0

You do not need the regular expression replaceAll; just use replace.

parsedJson = parsedJson.replace(putsValue.getKey(), putsValue.getValue());

1 Comment

I tried doesn't work. Thank you so much. I have huge respect for you.

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.