0

I'd like to use a JSON to store 3 values more comfortable. The Values I want to store are String, String and Boolean. Where the first string should be used to identify the 3 values from other in the JSON.

At the end - after the user set his setting I would expect the json to look something like this:

{

    "Room1": 
        {
                "IP": "192.168.0.2",
                "use": "true"
        },  
    "Bathroom":
        {
                "IP": "192.168.0.3",
                "use": "false"
        },  
    "Kitchen":
        {
                "IP": "192.168.0.4",
                "use": "true"
        }

        .
        .
        .
        .
}

At the end I'd like to "easily" retrieve those values and change some of them (set use to false, for example).

note: That JSON will be converted to a string and saved in SharedPreferences for later usage.

What would be a good way to implement this? are thery any good tutorials or recommendeds libraries?

If using a json at this point isn't a good idea how else should I do this?

Trinimon
  • 13,839
  • 9
  • 44
  • 60
globus243
  • 710
  • 1
  • 15
  • 31

1 Answers1

1

I think I'd rather use the following JSON:

{ "Rooms" : [
        {
                "Type" : "Room1",
                "IP"   : "192.168.0.2",
                "use"  : "true"
        },
        {
                "Type" : "Bathroom",
                "IP"   : "192.168.0.3",
                "use"  : "false"
        },
        {
                "Type" : "Kitchen",
                "IP"   : "192.168.0.4",
                "use"  : "true"
        }
    ]
}

with this POJO:

public class Room {

  @Expose
  private String Type;
  @Expose
  private String IP;
  @Expose
  private String use;

  public String getType() {
    return Type;
  }    

  public void setType(String Type) {
    this.Type = Type;
  }

  public String getIP() {
    return IP;
  }

  public void setIP(String IP) {
    this.IP = IP;
  }

  public String getUse() {
    return use;
  }

  public void setUse(String use) {
    this.use = use;
  }
}

added: Wrapping ArrayList for room list:

public class RoomList{

    @Expose
    private ArrayList<Room> Rooms = new ArrayList<Room>();

    public ArrayList<Room> getRooms() {
        return Rooms;
    }

    public void setRooms(ArrayList<Room> Rooms) {
        this.Rooms = Rooms;
    }
}

edited: and parse it vice versa with Gson:

String jsonString = "{ "Rooms" : [ { "Type" : "Room1", ... }, ... ] }";
RoomList rooms  = new RoomList();

Gson gson = new Gson();
rooms  = gson.fromJson(jsonString, rooms.getClass());

for (Room room : rooms.getRooms())
     System.out.println (room.getType());
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • could you please elaborate a bit more? What is POJO all I find is "*plain old java object*" and that "*it's nothing special*" quite confusing :) and where do you acutally work with the JSON? – globus243 Mar 05 '15 at 19:15
  • 1
    POJO is just an expression for a usual Java class - there is no restriction or rule it has to follow. Gson uses this class and it's instances to parse Json strings into objects and vice versa. I forgot the assignment above (will add it). `jsonString` in the example above is the JSON, e.g. assign your JSON string to `jsonString` before calling `fromJson` (will add this as well) – Trinimon Mar 05 '15 at 19:29
  • ah okay I get it. and if I want to add or delete a "room"? – globus243 Mar 05 '15 at 20:08
  • 1
    Looking at the String? Well, just add another room inside of the array `{ "Rooms": [ ..., { "Type" : "Livingroom", ... } ] }`. Adding or deleting from the `ArrayList`? Just use the `remove(Object o)` or `remove (int index)` method. Build the JSON string by calling `String json = gson.toJson(rooms); ` – Trinimon Mar 05 '15 at 20:37
  • just simply copy and pasted that code (instead of the `jsonString` of course. I made the Room class and an gsonWorker class (where I will ad more functions) and when I run it I get an `java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2` at `rooms = gson.fromJson(jsonString, rooms.getClass());` – globus243 Mar 05 '15 at 21:00
  • 1
    Mea culpa! I forgot that you have to wrap the class, if an `ArrayList` is involved, sorry for the confusion (see example here: https://paste.ee/p/ykWDT). Alternatively you can use an Array without class `RoomList` (see sample here: https://paste.ee/p/VOY8n) – Trinimon Mar 06 '15 at 09:52
  • okay, I've tinkered around a bit. And now I can easily get all the information from the JSON I need. thanks for that ! :) But what I stellt don't get is how to add a room ( with all the parameters - ip, use) I used your first example ( with the roomList class) I tried to add to the arraylist, but I think I'm just too knew to all that, to fully understand whats going on there. could you please add a snipped which explains how to add stuff and where to write it in a string? – globus243 Mar 07 '15 at 18:25
  • 1
    See this one: https://paste.ee/p/ACEnk. Just add a new room to the list, that's it. You can convert it to a JSON String by using the `toJson()` method. – Trinimon Mar 07 '15 at 21:55
  • May I annoy you one last time? As you said some comments above deleting things should be fairly easy, at least it looks like that. this is what I tried https://paste.ee/p/2T20X . but after all, the jsonString still contains the deleted object. what went wrong? I can't see any mistakes here. – globus243 Mar 08 '15 at 12:05
  • it's ok, I found the solution. Iterating and editing an ArrayList isn't possible, as stated in this question: http://stackoverflow.com/questions/9691328/removing-object-from-arraylist-in-for-each-loop . So I solved it like this https://paste.ee/p/wT07j – globus243 Mar 09 '15 at 11:42
  • Sorry, I saw your question a bit late: well, what you tried ends probably up in a `ConcurrentModificationException` because you changed the `ArrayList` while you are looping through it. What you could do as well is, that you add all items which you want to remove in a new list called _itemsToRemove_ and use `rooms.getRooms().removeAll(itemsToRemove)`. – Trinimon Mar 09 '15 at 13:12