xml 콘텐츠를 JSON 콘텐츠로 변환하고 있으며 콘텐츠는 다음과 같습니다.
{
"response":
{
"seatlist":
{
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
},
"userid":"8970ca285d9c4e4d",
"seatnum":12,
"session":"online"
}
}
다음과 같은 방법으로 사용자 ID와 좌석 번호를 얻을 수 있습니다.
JSONObject response = json.getJSONObject("response");
out.setUserid(response.getString("userid"));
out.setBalance(Double.valueOf(response.getString("balance")));
이제 문제는 다음 내용을 구문 분석하고 "num"값을 가져와야한다는 것입니다.
"seatlist":
{
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
}
여기에 내가 사용하는 코드가 있습니다.
JSONObject objList = response.getJSONObject ( "seatlist");
문자열 n = String.valueOf (objList.getJSONObject ( "seat"));
if (objList.getJSONObject ( "seat"). get ( "userId"). equals (userId)) {String num = String.valueOf (objList.getJSONObject ( "seat"). get ( "num")); out.setSeatNum (Integer.valueOf (num)); }
좌석 값이 하나 있으면 "num"값을 얻을 수 있습니다. 그렇지 않으면 JSON 예외가 발생합니다.
Pls는 나에게 이것에 대한 제안을 줘 .....
{
"response":
{
"seatlist":
{
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
},
"userid":"8970ca285d9c4e4d",
"seatnum":12,
"session":"online"
}
}
응답이라는 객체가있는 객체를 의미합니다. 여기에는 seatlist라는 객체가 포함됩니다. 이것은 seat라는 2 개의 객체를 포함합니다 (그러나 이것은 잘못되었습니다! 이것은 배열이어야합니다!). 등등..
이것을 읽으려면 사용할 수 있습니다.
JSONObject json = new JSONObject(yourresponse);
JSONObject response = json.getJSONObject("response");
JSONObject seatlist = response.getJSONObject("seatlist");
JSONObject userid = response.getJSONObject("userid");
JSONObject seatnum = response.getJSONObject("seatnum");
JSONObject session = response.getJSONObject("session");
이제 좌석 목록이 포함됩니다.
{
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
}
이름이 같은 요소가 2 개 포함되어 있기 때문에 잘못되었습니다. 이제 다음과 같이 인덱스로 호출 할 수 있습니다.
JSONObject seat1 = seatlist.getJSONObject(1);
JSONObject seat2 = seatlist.getJSONObject(2);
seat1.getString("balanace"); seat1.getInt("num");
또는 JSONObject를 반복 할 수 있습니다.
최소한 JSONOBject 대신 배열이어야합니다.
이것은 다음과 같이 보일 것임을 의미합니다.
{
"response": {
"seatlist": [
"seat":
{
"balance":85694.6,"num":12
},
"seat":
{
"balance":85694.6,"num":12
}
],
"userid":"8970ca285d9c4e4d",
"seatnum":12,
"session":"online"
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다