3

im having some trouble parsing json. I have json in the format of:

{"blah":"blah","blah":"blah"}
{"blah":"blah","blah":"blah"}
{"blah":"blah","blah":"blah"}

Here is the link to the JSON: http://gerrit.aokp.co/query?format=JSON&q=status:merged&age:1d

I cant make this a jsonobject and iterate over it. I currently have it as a string.

Is there a way to iterate over this? there will be over 500.

I tried making it an array by adding square brackets around it, but it didnt work because i needed to divide them with commas. I cant manipulate this by hand because im getting it from the web. So i tried this.

jsonString = jsonString.replaceAll("}(?!,)", "},");

the reason im adding the negative comma is that sometimes i might have a jsonobject inside of of these objects so I only want to add a comma in front of the '}' without commas.

when i do the replaceall i get this error.

Error in fetching or parsing JSON: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1:
    }(?!,)
    ^

What am I doing wrong or is there an easier way to do this that im looking over?

EDIT: Oh yes, I need to implement this in java because this is in an android app.

7
  • have you tried JSON.parse("your string"); Commented Aug 12, 2013 at 2:11
  • should it return a jsonobject or jsonarray? Commented Aug 12, 2013 at 2:12
  • I use org.codehaus.jettison.json.JSONObject and it works. Pass the string to the JSONObject constructor. The JSONObject class gives an Iterator of keys which you can use to get the values. Commented Aug 12, 2013 at 2:15
  • Android Studio cant seem to resolve "JSON" Commented Aug 12, 2013 at 2:15
  • Do the objects all end with a newline? Commented Aug 12, 2013 at 2:22

3 Answers 3

2

here is an example how you can accomplish what you want using Jackson's ObjectMapper.

ObjectMapper om = new ObjectMapper();
try {
    List<Object> obj = om.readValue(yourJsonString, new TypeReference<List<Object>> () { });
} catch (IOException e1) {
    e1.printStackTrace();
}

Now you will have a list of each of the individual Objects in your JSON string. To take it a step further you could create a POJO for the Object you are parsing.

Something like:

public class MyObject{
    private String project;
    private String branch;
}

That is just an exmple, you would need to define a property for each json property.

Then you can turn :

List<Object> obj = om.readValue(yourJsonString, new TypeReference<List<Object>> () { });

Into

List<MyObject> obj = om.readValue(yourJsonString, new TypeReference<List<MyObject>> () { });

Hope this helps!

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

Comments

1

From the link you posted, it looks like there are newlines between objects (and only between objects). If that's right, I'd approach it like this:

String[] items = dataFromWeb.split("\n");
String asJSONArrayString = Arrays.toString(items);
JSONArray jsonArray = new JSONArray(asJSONArrayString);

This splits the data at newlines, then joins it together with commas between elements and brackets around the whole thing.

2 Comments

This is probably the most practical approach. However, the best approach would be to get that Web API to return a well-formed JSON array of JSON objects.
@StephenC - I agree about getting the Web API to behave. If you ask for JSON, it should return valid JSON, not some garbage that looks vaguely like JSON but doesn't parse.
0
JSONObject jObject = null;
mJsonString = downloadFileFromInternet(urlString);
jObject = new JSONObject(mJsonString); 

This will get you json object.

This is the way to get json array from json object:

JSONArray jsonImageArray = jObject.getJSONArray("your string");

1 Comment

Yes, I know about this, but i can't make an object out of this because there are multiple.

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.