1

i wanted to know how I can convert this string here to an array:

[{"title":"test","birth":"20.05"},{"title":"test","birth":"13.05"},{"title":"test","birth":"13.06"},{"title":"test","birth":"23.06"},{"title":"test","birth":"01.12"},{"title":"test","birth":"01.06"}]

I already found this here:

 JSONObject jsnobject = new JSONObject(readlocationFeed);
JSONArray jsonArray = jsnobject.getJSONArray("locations");
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject explrObject = jsonArray.getJSONObject(i);

But if I try to make the object I always get null. And here:

JSONArray jsonArray = jsnobject.getJSONArray("locations");

I don't know what I should enter as "locations". Should I enter "[]" ?

Thanks really much for help!

Daniel

2 Answers 2

1

I don't know what I should enter as "locations". Should I enter "[]" ?

Nothing readlocationFeed is already a JSONArray. You just need

JSONArray jsonArray = new JSONArray(readlocationFeed);

without

JSONObject jsnobject = new JSONObject(readlocationFeed);
JSONArray jsonArray = jsnobject.getJSONArray("locations");
Sign up to request clarification or add additional context in comments.

1 Comment

would be honored if you take a look at my question ... :)
0

Shouldn't it be the other way around?

JSONArray jsnArray = new JSONArray(readlocationFeed);

Anyway I suggest you should use GSON for this kind of problem:

Gson gson = new Gson();
MyClass[] arr = gson.fromJson(str, MyClass[].class);

class MyClass{
    double birth;
    String title;       
}

Comments

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.