문제

그래서 나는 httpresponse에서 JSON 배열을 받고 있습니다. 배열은 다음 객체/변수로 구성됩니다.

{ "sessid": "vxkexkmbumbbyesrlvaxrxsaftfhdqd8", "session_name": "sess88cdfb2f1c420898", "user": { "uid": "60", "이름": "abc", "테마": "", "" ":" ":" ":" ":"Sess88cdfb2f1c420898 ": {"uid " ","signature_format ":"filtern_html ","생성 ":"13082976 ","Access ":"1386287 ","login ": 1386211,"status ":"1 ","timezone ": null,"언어 ": "", "그림": null, "data": { "mimemail_textonly": 0}, "rocel": { "2": "인증 된 사용자", "5": "Center User"}, "Field_centre_reference": { "und": [{ "nid": "256"}]}, "field_first_name": { "und": [{ "value": "web", "format": "safe_value": "web"} ]}, "field_surname": { "und": [{ "value": "services", "format": null, "safe_value": "services"}]}, "bounce_mail_blocked": "force_password_change": " 0 "}}

이제이 모든 객체/문자열을 별도의 변수로 받고 싶습니다. "sessid"를 변수 문자열 session_id에 저장하고 싶습니다. 등등. 다음 코드의 도움으로 첫 두 가지 (예 : sessid and session_name)를 간단한 방식으로 얻을 수 있습니다.

response = client.execute(httppost);

BasicResponseHandler handler = new BasicResponseHandler();
String data = handler.handleResponse(response);
jObj = new JSONObject(data);

sessid = jObj.getString("sessid");
Log.d("sessid obj", sessid);
session_name = jObj.getString("session_name");
Log.d("session_name", session_name);

그러나 저는 Android에서 멍청이이기 때문에 나머지 데이터를 변수로 저장하는 방법을 모르겠습니다. 다가오는 데이터는 간단한 방식으로 저장할 수 없습니다.

도움이 되었습니까?

해결책

이것으로 시도하십시오 :

 JSONObject j_user = jObj.getJSONObject("user");
 String uid = j_user.getString("uid");
 ...
 //And so on with the rest of the fields

다른 팁

{ // json object node 
    "sessid": "vxkEXkMBUmBByESRlvaxrxSaFTfhDqd8",
    "session_name": "SESS88cdfb2f1c420898",
    "user": { // json object user 
        "uid": "60", // string 
        "name": "abc",
        "theme": "",
        "signature": "",
        "signature_format": "filtered_html",
        "created": "13082976",
        "access": "1386287",
        "login": 1386211,
        "status": "1",
        "timezone": null,
        "language": "",
        "picture": null,
        "data": { // json object data
            "mimemail_textonly": 0 //string
        },
        ....// rest of the json

{ JSON 객체 노드를 나타냅니다

[ JSON 배열 노드를 나타냅니다

구문 분석

 JSONObject jObj = new JSONObject(load());
 String sessid = jObj.getString("sessid");
 Log.d("sessid obj", sessid);
 JSONObject user = jObj.getJSONObject("user");
 String uid = user.getString("uid"); 
 Log.d("sessid obj", uid);

데이터를 구문 분석합니다

 "data": {
        "mimemail_textonly": 0
    },


   JSONObject data= user.getJSONObject("data");
   Log.i(".......",""+data.getString("mimemail_textonly"));

Parser Field_centre_reference에

 "field_centre_reference": { // json object field_centre_reference
            "und": [  // json array und
                {        // json  object node 
                    "nid": "256" //string
                }
            ]
        },

  JSONObject field= user.getJSONObject("field_centre_reference");
  JSONArray jr = field.getJSONArray("und");
  JSONObject jb1 = (JSONObject) jr.get(0);
  Log.i(".......",""+jb1.getString("nid"));

JacksonParser 라이브러리를 확인하십시오. 작업을 쉽게하는 주석을 제공합니다. 그리고 그것은 가장 빠른 구문 분석 라이브러리 중 하나입니다 ... 당신은 그것을 가질 수 있습니다 여기

편집하다:예를 들어 내 이전 질문 중 하나를 살펴볼 수 있습니다. JacksonParser Databind 및 Core Cause "APK 용 중복 파일을 찾았습니까?"

당신이받은 JSON은 중첩 된 JSON임을 알아야합니다. 지도의 값 중 하나가지도라는지도와 같습니다. 귀하의 경우, jsonobject에는 sessid와 session_name이 포함되어 있지만 "사용자"도 jsonobject이므로 레벨별로 구문 분석 할 수 있습니다. 자세한 내용은 읽을 수 있습니다 http://www.vogella.com/articles/androidjson/article.html

이 시도..

구하는

 "user": {
        "uid": "60",

JSONObject obj_user = jObj.getJSONObject("user");
String uid = obj_user.getString("uid");

 "user": {
            "uid": "60",
"data": {
            "mimemail_textonly": 0
        }

JSONObject obj_data = jObj.getJSONObject("data");
String mimemail_textonly = obj_user.getString("mimemail_textonly");
JSONObject obj_roles = jObj.getJSONObject("roles");
String two = obj_roles.getString("2");

"field_centre_reference": {
            "und": [
                {
                    "nid": "256"           

JSONObject obj_field_centre_reference = jObj.getJSONObject("field_centre_reference");
JSONArray ary_und = obj_field_centre_reference.getJSONArray("und");
JSONObject objve = ary_und.getJSONObject(0);
String nid= objve.getString("nid");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top