How to handle a JSON format string?

Asked

Viewed 309 times

4

Let’s say I receive the following content that is stored in a String:

{
    "client_id": 1580,
    "videos": 4,
    "remote_urls": [{
            "url": "rtsp://aniceurl.com"
        },
        {
            "url": "rtsp://aniceurl.com"
        },
        {
            "url": "rtsp://aniceurl.com"
        },
        {
            "url": "rtsp://aniceurl.com"
        }
    ],
    "action": "start"
}

I recognize JSON and everything, but he’s saved in a String, not a Jsonobject. I would like to know how I can extract the information from it and store it in variables/arrays, so I can work with this data.

[]'s!

1 answer

4


you can use the Gson from google, it’s super simple, just create a class that has exactly the same attributes of json and parse, look at the example I did with your json, just using the method fromJson(meuJson, MinhaClasse.class):

Clientedto

import java.util.List;

public class ClienteDTO {
    private Integer client_id;
    private Integer videos;
    private List<RemoteUrlDTO> remote_urls;
    private String action;

    public Integer getClient_id() {
        return client_id;
    }
    public void setCliente_id(Integer cliente_id) {
        this.client_id = cliente_id;
    }
    public Integer getVideos() {
        return videos;
    }
    public void setVideos(Integer videos) {
        this.videos = videos;
    }
    public List<RemoteUrlDTO> getRemote_urls() {
        return remote_urls;
    }
    public void setRemote_urls(List<RemoteUrlDTO> remote_urls) {
        this.remote_urls = remote_urls;
    }
    public String getAction() {
        return action;
    }
    public void setAction(String action) {
        this.action = action;
    }
    @Override
    public String toString() {
        return "ClienteDTO [cliente_id=" + client_id + ", videos=" + videos + ", remote_urls=" + remote_urls
                + ", action=" + action + "]";
    }
}

Remoteurldto

public class RemoteUrlDTO {
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return "RemoteUrlDTO [url=" + url + "]";
    }
}

Class to test and demonstrate GSON:

import com.google.gson.Gson;

public class Main {
    public static void main(String[] args) {
        Gson g = new Gson();
        String json = "{\n" +
                "    \"client_id\": 1580,\n" +
                "    \"videos\": 4,\n" +
                "    \"remote_urls\": [{\n" +
                "            \"url\": \"rtsp://aniceurl.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"url\": \"rtsp://aniceurl.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"url\": \"rtsp://aniceurl.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"url\": \"rtsp://aniceurl.com\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"action\": \"start\"\n" +
                "}\n" +
                "\n";


        ClienteDTO clienteDTO = g.fromJson(json, ClienteDTO.class);

        System.out.println(clienteDTO);
    }
}

Result after execution of the test

ClienteDTO [cliente_id=1580, videos=4, remote_urls=[RemoteUrlDTO [url=rtsp://aniceurl.com], RemoteUrlDTO [url=rtsp://aniceurl.com], RemoteUrlDTO [url=rtsp://aniceurl.com], RemoteUrlDTO [url=rtsp://aniceurl.com]], action=start]
  • I think I understand how to use it! Could you explain to me just the "fromJson(meuJson, Minhaclasse.class)" part? How does it work? What class should I put as a parameter? Thank you!

  • 1

    Simple, in the fromJson method it expects two parameters, the first is a string that contains your json and the other is the class that has the same attributes as the.

  • 1

    vc does not need to touch this remoteurldto, because moving will break the parser of json to object, what you can do is access the url object like this : for (int i = 0; i < numVideo; i++) { mediaPlayer[i]. playMedia(clientDTO.getRemote_urls(). get(i). geturl()); }

  • 1

    I am assuming that you received a json and parsed it for the client class, so you should use that same instance to do this procedure.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.