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

TheAppService
  • 133
  • 1
  • 1
  • 12

2 Answers2

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");
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
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;       
}
Adam Arold
  • 29,285
  • 22
  • 112
  • 207