-4

I have following string :

{"attribute1":"value1","attribute2":"value2","attribute3":{"attribute3a":"value3a","attribute3b":"value3b"}},{"attribute1":"value1","attribute2":"value2","attribute3":{"attribute3a":"value3a","attribute3b":"value3b"}},{"attribute1":"value1","attribute2":"value2","attribute3":{"attribute3a":"value3a","attribute3b":"value3b"}}

I need to split this string on "," between "}}" and "{" and put each result in a tab.

The result I want is :

tab[0] : {"attribute1":"value1","attribute2":"value2","attribute3":{"attribute3a":"value3a","attribute3b":"value3b"}}
tab[1] : {"attribute1":"value1","attribute2":"value2","attribute3":{"attribute3a":"value3a","attribute3b":"value3b"}}
tab[2] : {"attribute1":"value1","attribute2":"value2","attribute3":{"attribute3a":"value3a","attribute3b":"value3b"}}

How can I do it?

Kcvin
  • 5,073
  • 2
  • 32
  • 54
Grichka
  • 53
  • 1
  • 11
  • 2
    this looks like JSON. I'd take a look at a JSON reader rather than trying to parse it yourself – RNJ Aug 02 '16 at 15:22

2 Answers2

0

Following code solves your problem

public class Test {

    public static void main(String args[]) {
        String input = "{\"attribute1\":\"value1\",\"attribute2\":\"value2\",\"attribute3\":{\"attribute3a\":\"value3a\",\"attribute3b\":\"value3b\"}},{\"attribute1\":\"value1\",\"attribute2\":\"value2\",\"attribute3\":{\"attribute3a\":\"value3a\",\"attribute3b\":\"value3b\"}},{\"attribute1\":\"value1\",\"attribute2\":\"value2\",\"attribute3\":{\"attribute3a\":\"value3a\",\"attribute3b\":\"value3b\"}}";
        String[] output = input.split("},");
        for(String s: output) {
            System.out.println(s+"}");
        }
    }

}

However, this is not an elegant way of doing it. You should try parsing your input using JSON libraries

JavaHopper
  • 5,567
  • 1
  • 19
  • 27
  • Welcome to StackOverflow. upvoting is the way of saying thanks here :-) – JavaHopper Aug 02 '16 at 15:58
  • Yes, I know but I can't. When I click on up arrow, a popup tell this : "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." Sorry ! :/ – Grichka Aug 25 '16 at 14:41
-1

This definitely looks like JSON. I would suggest looking at this SO post on parsing JSON: How to parse json in java

Community
  • 1
  • 1