5

I have used both ArrayList<> and JsonArray as the data sets in the adapter. One of the major difference I found was that the data set changes for the local data in Adapter was not reflected back in the calling class for JsonArray.

I wanted to know using which one is better and is my observation correct.

Also if my data set involves extracting data from a webservice(that gives me Json form data) will my changing it to ArrayList<> be better.

Harshit
  • 623
  • 7
  • 22
  • Could you please elaborate , what do you mean by " data set changes for the local data in Adapter was not reflected back in the calling class for JsonArray" ? – Mithun Sarker Shuvro Jul 07 '16 at 23:13
  • Yes. I have a data set in MainActivity. I am passing that data to Adapter class using a function and then calling notifyDataSetChanged(). The changes in that data set I make are not synchronized when using JsonArray but they are synchronized when using ArrayList<>. – Harshit Jul 08 '16 at 04:28
  • @Harshit does your JsonArray contains more structured data? – JJ86 Jul 08 '16 at 15:42
  • It is usually a jsonArray of jsonObjects.@jj86 – Harshit Jul 09 '16 at 19:44

6 Answers6

4

JsonArray implemented by ArrayList under the hood. http://grepcode.com/file/repo1.maven.org/maven2/org.json/json/20080701/org/json/JSONArray.java

So its very similar variants for app performance. Use what you like =)

mlevytskiy
  • 1,555
  • 2
  • 16
  • 26
2

As far as I know JsonArray isn't much slower than ArrayList. So I believe that you should be ok using JsonArray. We would need to see some code to help out with the problem when using jsons.

1

JsonArray internally uses ArrayList, so the delay is less because internal calling is fast in Java.

But if you convert JsonArray to ArrayList and JsonArray is contained more complex data then accessing of ArrayList is fast as per developer easyness and performance of App.

Asif Patel
  • 1,744
  • 1
  • 20
  • 27
1

Well jsonArray is a fast solution so you can definitely use it as your data sets and still if you want to convert JsonArray into Array list for gaining all methods available for it then read this answer

https://stackoverflow.com/a/17037364/5476209

In your case its fine to go with JsonArray but a good way and strongly recommend way is use GSON I would say a golden approach whenever you working with Json I would damn prefer it, see this answer.

Google Gson - deserialize list<class> object? (generic type)

Community
  • 1
  • 1
TapanHP
  • 5,969
  • 6
  • 37
  • 66
1

JSONArray internally uses ArrayList. It's just wrapper over ArrayList. So I'd say there is no difference. JSONObject uses HashMap so again no real drawbacks.

Suhas Bachewar
  • 1,230
  • 7
  • 21
1

Yes they both can be used. As to which is more correct, Using a JsonArray feels like it breaks MVC. So if tomorrow you changed from using Json to XML, yaml, etc. you would have to change your model. Ideally, your model should know the bare minimum of what is needed to display the views and be coupled to nothing but the views. If you want to display three labels, then your model should only contain three strings. So, I would recommend that you make it a List.

David Stocking
  • 1,200
  • 15
  • 26