0

I have a JSON String, but I need to change the names of the keys. How can I do this? Is it possible to change them directly without parsing the JSON to a HashMap or something?

1
  • 1
    not the best way, but you can do jsonString.replaceAll("\"oldKey\"", "\"newKey\""); Commented Feb 12, 2013 at 13:55

1 Answer 1

2

only via an ugly regex that might not be very successful -

{ "foo" : "bar", "bar" : "foo"}

is going to cause you problems.

I would parse to an object format (like hashmap as you suggest) - and then do your search and replace on that - I think that it will be much more accurate.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't beleave that a regex could work, because the key may also be an value. And the value should not be replaced.

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.