저는 Gson 구문 분석을 처음 사용했으며 기본적인 Gson 구문 분석을 거의 수행하지 않았습니다. 그러나 이번에는 JSON이 훨씬 복잡합니다. 내 JSON은 다음과 같습니다.
{"uname":"man101",
"uid":"2",
"account":{
"entry":[8,15.48],
"exit":[8,15.48],
"details":
[[0,0],[0,8.2],[1.15,8.2],[1.15,18.23],[7.33,18.23],[7.33,15.48],[12.15,2.28],
[12.35,2.28],[12.35,0],[10.65,0],[10.65,1.42],[8.1,1.42],[8.1,3.95],
[4.25,3.95],[4.25,0]],
"section":
[
{
"account":[[0,0],[0,3.35],
[4.25,3.35],[4.25,0]],
"category":"office",
"description":"Mobile based company",
"sectionname":"xyz",
"id":1
},
{
"account":[[0,3.95],[0,7.8],
[4.25,7.8],4.25,3.95]],
"category":"office",
"description":"Network based company",
"sectionname":"ABC",
"id":2
},
]
},
"category":"Cowork",
"description":"Combined office space"
}
그리고 나는 이것을 다음과 같은 방식으로 파싱하려고했습니다.
public class AccountData
{
public String uname;
public String uid;
public String category;
public String description;
public Account account;
public class Account
{
public float[] entry;
public float[] exit;
public List<float[]> details;
public List<Section> section;
}
public class Section
{
public List<float[]> account;
public String category;
public String description;
public String sectionname;
public String id;
}
}
그리고 이렇게 결과를 전달하려고
Gson gson = new Gson();
beaconList = gson.fromJson(result, AccountData.class);
오류없이 실행되지만 일부 데이터에 액세스하려고하면 null 값이 제공됩니다.
우선 , JSON이 잘못되었습니다. 여기에 수정 된 버전이 있습니다 (예를 들어 코드의 9 행에 추가 쉼표가 있음).
{
"uname": "man101",
"uid": "2",
"account": {
"entry": [
8,
15.48
],
"exit": [
8,
15.48
],
"details": [
[
0,
0
],
[
0,
8.2
],
[
1.15,
8.2
],
[
1.15,
18.23
],
[
7.33,
18.23
],
[
7.33,
15.48
],
[
12.15,
2.28
],
[
12.35,
2.28
],
[
12.35,
0
],
[
10.65,
0
],
[
10.65,
1.42
],
[
8.1,
1.42
],
[
8.1,
3.95
],
[
4.25,
3.95
],
[
4.25,
0
]
],
"section": [
{
"account": [
[
0,
0
],
[
0,
3.35
],
[
4.25,
3.35
],
[
4.25,
0
]
],
"category": "office",
"description": "Mobile based company",
"sectionname": "xyz",
"id": 1
},
{
"account": [
[
0,
3.95
],
[
0,
7.8
],
[
4.25,
7.8
],
[
4.25,
3.95
]
],
"category": "office",
"description": "Network based company",
"sectionname": "ABC",
"id": 2
}
]
},
"category": "Cowork",
"description": "Combined office space"
}
http://json.parser.online.fr/ 또는 http://www.bodurov.com/JsonFormatter/에서 json을 확인할 수 있습니다 .
둘째 , Gson은 정적으로 선언되지 않는 한 내부 클래스를 그다지 좋아하지 않습니다.
그리고 세 번째 다음과 같이 나는 당신의 클래스를 재정의 있도록 피하기 수업에 배열과 제네릭을 혼합, 제네릭은 사용에 안전하다 :
public class AccountData {
public String uname;
public String uid;
public String category;
public String description;
public Account account;
public static class Account {
public List<Double> entry;
public List<Double> exit;
public List<List<Double>> details;
public List<Section> section;
}
public static class Section {
public List<List<Double>> account;
public String category;
public String description;
public String sectionname;
public String id;
}
}
당신이 내부 정적 클래스 마음에 들지 않으면 당신은 항상 넣을 수 있습니다 Section
및 Account
별도의 파일로 (물론, static 키워드없이).
편집하다
Brian Roach가 의견에서 지적했듯이 Gson과 잘 작동하기 위해 내부 클래스가 더 이상 정적 일 필요가 없습니다. 따라서 포인트 2는 더 이상 사실이 아닙니다. 클래스 선언에서 정적을 제거 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다