K
K
kambulovd2019-11-25 23:05:56
Java
kambulovd, 2019-11-25 23:05:56

How to convert json file (JAVA)?

Hello. json file is coming.

{
  "response": {
    "count": 22687,
    "items": [
      {
        "id": 457263017,
        "album_id": -7,
        "owner_id": -89600892,
        "user_id": 100,
        "sizes": [
          {
            "type": "m",
            "url": "https://sun1-

90.userapi.com/c850424/v850424557/xxx/BJTxSEZNPg.jpg",
            "width": 97,
            "height": 130
          },
          {
            "type": "s",
            "url": "https://sun1-

84.userapi.com/c850424/v850424557/xxx/TqxyEuXzs4.jpg",
            "width": 56,
            "height": 75
          },
          {
            "type": "w",
            "url": "https://sun1-

15.userapi.com/c850424/v850424557/ee1x5/dx4c4pOL7Q.jpg",
            "width": 1620,
            "height": 2160
          }
          
        ],
        "text": "",
        "date": 1570167720,
        "lat": 46.3238,
        "long": 38.2321
      }
  }

How can I represent it as a string using java? because only in this format I can upload to the database
{"response":{"count":22687,"items":[{"id":457223263017,"album_id":-7,"owner_id":-89600892,"user_id":100,"sizes": [{"type":"m","url":"https://sun1-
90.userapi.com/c850424/v850424557/xxx/BJTxSEZNPg.jpg","width": 97,"height": 130},{"type": "s".........}}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2019-11-26
@kambulovd

kambulovd if you have not broken json and you are not going to throw anything out of it, then it is very simple

JsonParser parser = new JsonParser();
    JsonObject jsonObject = parser.parse(ваша строка)
        .getAsJsonObject();
    assertTrue(jsonObject.isJsonObject());
    String result = jsonObject.toString();
    System.err.println("Result: " + result);

and if you are going to throw something out, then use gson GsonBuilder and gson.stream.JsonWriter (to find out exactly how to describe exactly what you want to throw out)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question