0

I've below json value in my StringBuilder variable, I want to parse all id key value and store it again in StringBuilder.

{"status":"success","id":"1"}
{"status":"success","id":"2"}
{"status":"success","id":"3"}
{"status":"success","id":"4"}
{"status":"success","id":"5"}
{"status":"success","id":"6"}

Expected output: 1 2 3 4 5 6

How can I parse these value in java?

I tried below option but it doesn't help:

StringBuilder str = new StringBuilder();
str.append(jsonStringValue);

JSONObject jObj = new JSONObject(str);
jObj.getString("id");

Using JSONTokener

JSONTokener t = new JSONTokener(str.toString());
 while (t.more()) {
JSONObject o2 = (JSONObject) t.nextValue();
System.out.println(o2.getString("id"));
}

But I'm getting below error message: org.json.JSONException: Missing value at character 128

java begineer
  • 319
  • 5
  • 15
  • 1
    that is not valid JSON, each line is valid, but the entire thing is not –  Feb 25 '16 at 21:56
  • Exact Duplicate; One of Many: http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java?rq=1 –  Feb 25 '16 at 21:57

2 Answers2

0

If you're using org.json, You can use JSONTokener.

Here's example shows how it works.

public static void main(String args[]) throws JSONException {
    String str1 = "{\"strValue\":\"string\"}\n{\"intValue\":1}";
    JSONTokener t = new JSONTokener(str1);
    JSONObject o1 = (JSONObject) t.nextValue();
    JSONObject o2 = (JSONObject) t.nextValue();
    System.out.println(o1.getString("strValue"));
    System.out.println(o2.getLong("intValue"));
    System.out.println(t.more()); // Check if there's more token. can be used to process with loop.
}

Or if you can change input string, you can put those object into Json array.

[
{"status":"success","id":"1"},
{"status":"success","id":"2"},
{"status":"success","id":"3"},
{"status":"success","id":"4"},
{"status":"success","id":"5"},
{"status":"success","id":"6"}
]

In that case you can use org.json.JSONArray to handle it.

Satoshi
  • 74
  • 6
  • Hi, I tried you approach but I'm getting this exception org.json.JSONException: Missing value at character 128 . I've updated my code in original post.. can you correct me if i'm doing something wrong – java begineer Feb 25 '16 at 22:58
  • Did you escape double quotation? Should be like builder.append("{\"status\":\"success\",\"id\":\"1\"}"); – Satoshi Feb 25 '16 at 23:02
0

You can use regexps like this

public class Test {

public static void main(String[] args) {
    StringBuilder inputBuf = prepareStringBuilder();

    StringBuilder outputBuf = new StringBuilder();

    Pattern pattern = Pattern.compile(":\"(\\d+)\"");
    Matcher matcher = pattern.matcher(inputBuf);
    while (matcher.find()) {
        String group = matcher.group(1);
        outputBuf.append(group);
    }

    System.out.println(outputBuf);
}

private static StringBuilder prepareStringBuilder() {
    StringBuilder buf = new StringBuilder();
    buf.append("{\"status\":\"success\",\"id\":\"1\"}");
    buf.append("{\"status\":\"success\",\"id\":\"2\"}");
    buf.append("{\"status\":\"success\",\"id\":\"3\"}");
    buf.append("{\"status\":\"success\",\"id\":\"4\"}");
    buf.append("{\"status\":\"success\",\"id\":\"5\"}");
    buf.append("{\"status\":\"success\",\"id\":\"6\"}");
    return buf;
}
}
Andriy Kryvtsun
  • 3,220
  • 3
  • 27
  • 41