java解析json格式資料
阿新 • • 發佈:2019-01-29
有時候可能會用到json格式進行資料的傳輸,那麼怎麼把接收到的資料解析出來呢? 下面介紹兩種解析json資料的方法:
1、通過谷歌的Gson來進行解析:
json資料:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};
兩個VO類:
2、通過json-org.jar包進行解析:
json資料:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};
一個VO類:
1、通過谷歌的Gson來進行解析:
json資料:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};
兩個VO類:
-
public class SurveyVO {
-
private String surveyId;
-
private String surveyName;
-
private
-
public String getMessage() {
-
return message;
-
}
-
public void setMessage(String message) {
-
this.message = message;
-
}
-
public String getSurveyId() {
-
return surveyId;
-
}
-
public void setSurveyId(String surveyId) {
-
this.surveyId = surveyId;
-
}
-
public String getSurveyName() {
-
return surveyName;
-
}
-
public void setSurveyName(String surveyName) {
-
this.surveyName = surveyName;
-
}
-
}
-
public class SurveyListVO {
-
private String message;
-
private List<SurveyVO> result;
-
public String getMessage()
-
return message;
-
}
-
public void setMessage(String message) {
-
this.message = message;
-
}
-
public List<SurveyVO> getResult() {
-
return result;
-
}
-
public void setResult(List<SurveyVO> result) {
-
this.result = result;
-
}
- }
-
public class fromGson() {
-
sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{"surveyid":"2","surveyname":"C"}]};
-
Gson gson = new Gson();
-
SurveyListVO surveyListVO = gson.fromJson(sTotalString,
-
SurveyListVO.class);
-
for (int i = 0; i < surveyListVO.getResult().size(); i++) {
-
System.out.print(surveyListVO.getResult().get(i)
-
.getSurveyId());
-
print: 1////2
-
System.out.print(surveyListVO.getResult().get(i)
-
.getSurveyName());
-
print: B///C
-
System.out.print(surveyListVO.getMessage());
-
}
- }
2、通過json-org.jar包進行解析:
json資料:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};
一個VO類:
-
public class SurveyVO {
-
private String surveyId;
-
private String surveyName;
-
public String getSurveyId() {
-
return surveyId;
-
}
-
public void setSurveyId(String surveyId) {
-
this.surveyId = surveyId;
-
}
-
public String getSurveyName() {
-
return surveyName;
-
}
-
public void setSurveyName(String surveyName) {
-
this.surveyName = surveyName;
-
}
- }
-
public class fromJson() {
-
sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{"surveyid":"2","surveyname":"C"}]};
-
JSONObject json;
-
try {
-
json = new JSONObject(sTotalString);
-
JSONArray results = json.getJSONArray("result");
-
for (int i = 0; i < results.length(); i++) {
-
SurveyVO surveyVO = new SurveyVO();
-
JSONObject result = results.getJSONObject(i);
-
System.out.println(result.getString("surveyId")+"
"+result.getString("surveyName"));
-
surveyVO.setSurveyId(result.getString("surveyId"));
-
surveyVO.setSurveyName(result.getString("surveyName"));
-
surveyVOList.add(surveyVO);
-
}
-
} catch (JSONException e) {
-
e.printStackTrace();
-
}
- }