2013-05-07 143 views
0

我得到的JSON響應我要分析此如何解析JSON響應的JSON數組中的Android

[ 
    { 
     "fbuid": "100000462110782" 
    }, 
    { 
     "fullname": "Arun Pathania" 
    }, 
    { 
     "fbuid": "100000257902867" 
    }, 
    { 
     "fullname": "Shiju vargheae" 
    }, 
    { 
     "fbuid": "100003337246078" 
    }, 
    { 
     "fullname": "Smart Buzz" 
    } 
] 

我試過如下:

JSONArray jArray = new JSONArray(result); 
for (int i=0; i < jArray.length(); i++) { 
    JSONObject oneObject = jArray.getJSONObject(i); // Pulling items from the array 
    u_id_json = oneObject.getString("fbuid"); 
    u_name_json = oneObject.getString("fullname"); 
} 

錯誤

error is coming is :--org.json.JSONException: No value for fullname – 

請告訴我如何解析此android

+2

很簡單,但你有什麼試過? – 2013-05-07 07:02:29

+0

在stackoverflow上有數百個例子,你是否先嚐試搜索? – 2013-05-07 07:03:17

+0

http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android http://stackoverflow.com/questions/9715856/parsing-this-json – 2013-05-07 07:05:01

回答

2
JSONArray main; 
    int i; 
    JSONObject mainobj,fb,fname; 
    String[] fbuid,fullname; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
      try { 
       main = new JSONArray(jsonstring); 
       Log.e(main.toString(),"string"); 
       fbuid=new String[main.length()]; 
       fullname=new String[main.length()]; 
       for(i=0;i<main.length();i++) 
       { 
        mainobj = main.getJSONObject(i); 
        if(mainobj.has("fbuid")) 
        { 
         fbuid[i]=mainobj.getString("fbuid"); 
         Log.e(fbuid[i],"string"); 
        } 
        else if(mainobj.has("fullname")) 
        { 
         fullname[i]=mainobj.getString("fullname"); 
         Log.e(fullname[i],"string"); 
        } 
       } 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

試試這個

+0

非常感謝..它的工作正常,但在最後給java.lang.ArrayIndexOutOfBoundsException任何想法? – Arun 2013-05-07 07:46:23

+0

在你的循環中使用<=條件,所以它給你錯誤。 – ishu 2013-05-07 07:54:52

+0

不,我沒有用過<=條件當我在日誌fullname [i] +「」+ fbuid [i]打印循環時,異常即將到來...我必須從這些字符串數組中獲取值嗎? – Arun 2013-05-07 08:20:13