0

i use gson to get parese json data.but i have some problems here is my json url data https://api.kcg.gov.tw/api/service/get/2c1d9959-d038-4918-bae3-409680f8193a

you can see that the json data have structure.

here is my code first

package iii_project;

import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class GsonFetchNetworkJson2 {
    public class ItemType {
        int seq;
        String 資料年度;
        String 統計項目;
        String 稅目別;
        String 資料單位;
        String 值;
//      public String toString() { 
//          return Arrays.toString(seq);
//      }
    }
    public class Item {
        String isImage;
        String success;
        String id;
        ItemType[] data;
        @Override
        public String toString() {
            return "Item [isImage=" + isImage + ", success=" + success + ", data=" + Arrays.toString(data) + "]";
        }
        
    }
    
    public static void main(String[] ignored) throws Exception {
//    Type listType = new TypeToken<List<Item>>() {}.getType();
       URL url = new URL("https://api.kcg.gov.tw/api/service/get/2c1d9959-d038-4918-bae3-409680f8193a");
       InputStreamReader reader = new InputStreamReader(url.openStream());
       Item dto = new Gson().fromJson(reader, Item.class);
       System.out.println(dto);
//       System.out.println(dto.isImage);
//       System.out.println(dto.id);
//       System.out.println(dto.success);

//       System.out.println(dto.types);
//       System.out.println(dto.data.toString());
//       System.out.println(dto.toString());
//       System.out.println(Arrays.toString(dto.date));
//      Detail abd = new Gson().fromJson(reader, Detail.class);
//      System.out.println(Arrays.toString(abd.值));
//      System.out.println(abd.資料單位);
//      System.out.println(Arrays.toString(dto.data));
    }
}

but the result of

System.out.println(dto);

i haved override it but still can not work enter image description here

i want the data like this: enter image description here

here is the answer!!!!

package iii_project;

import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class GsonFetchNetworkJson2 {
    public class ItemType {
        private int seq;
        private String 資料年度;
        private String 統計項目;
        private String 稅目別;
        private String 資料單位;
        private String 值;
        //getter and setter
        @Override
        public String toString() {
            return "ItemType [seq=" + seq + ", 資料年度=" + 資料年度 + ", 統計項目=" + 統計項目 + ", 稅目別=" + 稅目別 + ", 資料單位=" + 資料單位
                    + ", 值=" + 值 + "]";
        }
        

    }
    public class Item {
        String isImage;
        String success;
        String id;
        ItemType[] data;
        @Override
        public String toString() {
            return "Item [isImage=" + isImage + ", success=" + success + ", id=" + id + ", data="
                    + Arrays.toString(data) + "]";
        }
        
    }
    
    public static void main(String[] ignored) throws Exception {
//    Type listType = new TypeToken<List<Item>>() {}.getType();
       URL url = new URL("https://api.kcg.gov.tw/api/service/get/2c1d9959-d038-4918-bae3-409680f8193a");
       InputStreamReader reader = new InputStreamReader(url.openStream());
       Item dto = new Gson().fromJson(reader, Item.class);
//     System.out.println(dto);
//       System.out.println(Arrays.toString(dto.data));
       System.out.println(dto.isImage);
       System.out.println(dto);
//       for(ItemType element : dto.data) {
//           System.out.println(element);
//       }
//       System.out.println(dto.data);
//       System.out.println(dto.id);
//       System.out.println(dto.success);

//       System.out.println(dto.types);
//       System.out.println(dto.data.toString());
//       System.out.println(dto.toString());
//       System.out.println(Arrays.toString(dto.date));
//      Detail abd = new Gson().fromJson(reader, Detail.class);
//      System.out.println(Arrays.toString(abd.值));
//      System.out.println(abd.資料單位);
//      System.out.println(Arrays.toString(dto.data));
    }
}
陳寧寬
  • 33
  • 8
  • `Arrays.toString` will internally call `toString` on each inner object. Therefore, your Details class needs to have that method **and** you need to use `Arrays.toString` rather than just print out the array itself – OneCricketeer Mar 24 '21 at 05:24

3 Answers3

2

Your dto.data is array, so your current output is the address of that array. Replace your System.out.println(dto.data) by System.out.println(Arrays.toString(array)); to print your data array

Check out this to have more details: What's the simplest way to print a Java array?

Hưng Chu
  • 1,027
  • 5
  • 11
1

You need to override the toString function in your Detail class, otherwise it will only print out its address for the object.

0

but still can not work

It works exactly as you've written it to!

I want the data like this (shows image of some JSON object)

You need to explicitly write the indentation in your toString function, then

For example

public String toString() {
    StringBuilder sb = new StringBuilder("{\n");
    sb.append("  \"isImage\":" + isImage).append(",\n");
    sb.append("  \"data\":");
    if (data.length == 0) {
       // add empty array string "[]"
    } else {
        sb.append("[");
        for (ItemType it : data) {
           // create object string 
          // append to outer builder 
          // add a comma, but not for the very last object in the array 
        }
        sb.append("]");
    }
    // TODO: close off remaining end brackets 
    return sb.toString();
} 

And your ItemType class still needs it's own toString...

If you really just want to print indented JSON without much code, then don't try to print an Item class

Pretty-Print JSON in Java

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245