Monday, September 8, 2014

how to import/parse or create json/gson as arraylist from strings.xml in android?

Hi there!

Today i'm gonna show to you, how we can profit from the existing Gson library while dealing with array lists in the string.xml file. The benefits of it is that can define as many strings.xml entries as we need and import/parse the whole list structures almost without any effort.

Gson library

download the Gson library from here Gson 2.2.4. Don't worry about the "deprecated" tag. It is not really deprecated. Only parts of it acc. to its developer. We can continue using it. Than unzip it and cut/paste the following jar (gson-2.2.4.jar) in the libs folder from your project.

Json sample

Open your strings.xml file and copy/paste this line sample entry in it(It is a representation of an array list of Points with x and y coordinates):

 < string name="point_array" > {\"points\":[{\"x\": 255,\"y\": 689},{\"x\": 199,\"y\": 658}< / string > 

Defining a Data Container

You'll need a data container to hold the read points. So lets define a structure for it.
           
public class PointContainer {
    private List < Point > points;
    public List < Point > getPoints() {return points;}
    public void setPoints(List < Point > points) {this.points = points;}
}

Usage of the Gson lib

With the Gson lib, import/parse the array into a json structure. The benefits of it is that can define as many strings.xml entries as you need and import the whole list structure almost without any effort.
           
    String pointArray = getResources().getString(R.string.point_array);
    PointContainer pointContainer = new Gson().fromJson(pointArray, PointContainer.class);
    List < Point > points = pointContainer.getPoints();
    // do something with the data here...

Reading List of Lists

Here it gets more tricky. Offen we will need to read a list of list from json format into java code. To do so, lets see how it works by writing a simple example.We will do the same way as the code above. the only difference is to note that if we have list of list we will need to create a class that holds the sublists and so on...
 
< string name="string_points" >{"items":[{"points":[{"x": 644,"y": 420},{"x": 644,"y": 421},{"points":[{"x": 752,"y": 348},{"x": 752,"y": 349}]}]} < / string >

public class ItemContainer {
 private List < Item > items ;
 public List < Item > getItems() {
  return items 
 }
 public void setItems(List < Item > items ) {
  this.items  = items 
 }
}

public class Item {
 private List < Point > points;
 public List < Point > getPoints() {
  return points;
 }
 public void setPoints(List < Point > points) {
  this.points = points;
 }
}

String string = cxt.getResources().getString(R.string.string_points);
List < ItemContainer > l =  new ArrayList < ItemContainer >();
Gson gson = new Gson();
l.add(gson.fromJson(string, ItemContainer.class));

That's all! Hope you like it!

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†