JSONobject.parseObject(String json,Class clazz)를 사용 하여 JSon 데 이 터 를 분석 합 니 다.

27933 단어 자바json
예제 설명:
JSONobject.parseObject(json,클래스 이름.class)를 사용 하여 json 데 이 터 를 분석 하고 실체 류 분석 대상 은 JSon 데이터 의 대상 유형 에 따라 정의 할 수 있 으 며 다 층 대상 관 계 를 포함 하여 분석 할 수 있 으 며 해당 하 는 json 데이터 대상 의 등급 구 조 를 주의 하면 됩 니 다.
Json Jar 가방(maven 의존):
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.4</version>
</dependency>

JSon 데이터:
{
  "status": "1",
  "regeocode": {
    "addressComponent": {
      "city": "   ",
      "province": "   ",
      "adcode": "420106",
      "district": "   ",
      "towncode": "420106006000",
      "streetNumber": {
        "number": "46 ",
        "location": "114.300143,30.5515561",
        "direction": "  ",
        "distance": "32.3008",
        "street": "   "
      },
      "country": "  ",
      "township": "      ",
      "businessAreas": [
        {
          "location": "114.300581,30.547575",
          "name": "   ",
          "id": "420106"
        },
        {
          "location": "114.305741,30.583179",
          "name": "  ",
          "id": "420102"
        },
        {
          "location": "114.31082,30.560542",
          "name": "   ",
          "id": "420106"
        }
      ],
      "building": {
        "name": [
          
        ],
        "type": [
          
        ]
      },
      "neighborhood": {
        "name": [
          
        ],
        "type": [
          
        ]
      },
      "citycode": "027"
    },
    "formatted_address": "                              "
  },
  "info": "OK",
  "infocode": "10000"
}

분석 코드 작업 클래스:
import com.alibaba.fastjson.JSONObject;

/** 
 * @ClassName: GaoDeStruct 
 * @Description: TODO
 * @author: mzb
 * @date: 2020 3 18    3:57:34  
 */
public class GaoDeStruct {
	private String status;
	private String info;
	private String infocode;
	private Regeocode regeocode;
	

	public String getStatus() {
		return status;
	}


	public void setStatus(String status) {
		this.status = status;
	}


	public String getInfo() {
		return info;
	}


	public void setInfo(String info) {
		this.info = info;
	}


	public String getInfocode() {
		return infocode;
	}


	public void setInfocode(String infocode) {
		this.infocode = infocode;
	}


	public Regeocode getRegeocode() {
		return regeocode;
	}


	public void setRegeocode(Regeocode regeocode) {
		this.regeocode = regeocode;
	}


	public static void main(String[] args) {
		String json = "{" + 
				"  \"status\": \"1\"," + 
				"  \"regeocode\": {" + 
				"    \"addressComponent\": {" + 
				"      \"city\": \"   \"," + 
				"      \"province\": \"   \"," + 
				"      \"adcode\": \"420106\"," + 
				"      \"district\": \"   \"," + 
				"      \"towncode\": \"420106006000\"," + 
				"      \"streetNumber\": {" + 
				"        \"number\": \"46 \"," + 
				"        \"location\": \"114.300143,30.5515561\"," + 
				"        \"direction\": \"  \"," + 
				"        \"distance\": \"32.3008\"," + 
				"        \"street\": \"   \"" + 
				"      }," + 
				"      \"country\": \"  \"," + 
				"      \"township\": \"      \"," + 
				"      \"businessAreas\": [" + 
				"        {" + 
				"          \"location\": \"114.300581,30.547575\"," + 
				"          \"name\": \"   \"," + 
				"          \"id\": \"420106\"" + 
				"        }," + 
				"        {" + 
				"          \"location\": \"114.305741,30.583179\"," + 
				"          \"name\": \"  \"," + 
				"          \"id\": \"420102\"" + 
				"        }," + 
				"        {" + 
				"          \"location\": \"114.31082,30.560542\"," + 
				"          \"name\": \"   \"," + 
				"          \"id\": \"420106\"" + 
				"        }" + 
				"      ]," + 
				"      \"building\": {" + 
				"        \"name\": [" + 
				"          " + 
				"        ]," + 
				"        \"type\": [" + 
				"          " + 
				"        ]" + 
				"      }," + 
				"      \"neighborhood\": {" + 
				"        \"name\": [" + 
				"          " + 
				"        ]," + 
				"        \"type\": [" + 
				"          " + 
				"        ]" + 
				"      }," + 
				"      \"citycode\": \"027\"" + 
				"    }," + 
				"    \"formatted_address\": \"                              \"" + 
				"  }," + 
				"  \"info\": \"OK\"," + 
				"  \"infocode\": \"10000\"" + 
				"}";
	
		AddressComponent address = parse(json);
		System.out.println(address);
		//                  json           
//		System.out.println(JSONObject.toJSONString(address));
	}

	public static AddressComponent parse(String json) {
		GaoDeStruct struct = JSONObject.parseObject(json, GaoDeStruct.class);
		return struct.getRegeocode().getAddressComponent();
	}
}

JSon 데이터 패 키 징 대상 클래스:
import java.util.List;

public class AddressComponent {
	private String city;
	private String province;
	private String  adcode;
	private String district;
	private String towncode;
	private StreetNumber streetNumber;
	private String country;
	private String township;
	private List<BusinessArea> businessAreas;
	private Building building;
	private Neighborhood neighborhood;
	private String citycode;
	//get/set/equals/hashCode/toString    
}

import java.util.List;

public class Building {
	private List<String> name;
	private List<String> type;
	//get/set/equals/hashCode/toString    
}

public class BusinessArea {
	private String location;
	private String name;
	private String id;
	//get/set/equals/hashCode/toString    
}

import java.util.List;

public class Neighborhood {
	private List<String> name;
	private List<String> type;
	//get/set/equals/hashCode/toString    
}

public class Regeocode {
	
	private AddressComponent addressComponent;
	private String formatted_address;
	//get/set/equals/hashCode/toString    
}

public class StreetNumber {
	private String number;
	private String location;
	private String direction;
	private String distance;
	private String street;
	//get/set/equals/hashCode/toString    
}

좋은 웹페이지 즐겨찾기