2014-12-06 90 views
0

jsonarray值這是我JSON編碼碼如何顯示在安卓

{"detail": 
     {"name":["The name field is required."], 
     "password":["The password field is required."], 
     "email":["The email field is required."], 
     "phone":["The phone field is required."], 
     "address":["The address field is required."]},"success":0} 

如何轉換成JSONObject的。例如"name":["The name field is required."]我想向用戶顯示名稱的價值任何人都可以告訴我如何做到這一點。

這是我的Android代碼

public class AccountRegister extends Activity { 
private ProgressDialog pDialog; 
String password; 
Button bnt_Submit; 
EditText edt_email,edt_password,edt_name,edt_phone,edt_address; 

JSONParser jsonParser = new JSONParser(); 

private static String url_create_product = "http://192.168.1.2/laravel/public/registerUser"; 


private static final String TAG_SUCCESS = "success"; 
    @Override 


protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.register); 


bnt_Submit =(Button)findViewById(R.id.btnsubmit); 
edt_email = (EditText)findViewById(R.id.Edt_Email); 
edt_password = (EditText)findViewById(R.id.Edt_Password); 
edt_name = (EditText)findViewById(R.id.Edt_Name); 
edt_phone = (EditText)findViewById(R.id.Edt_Phone); 
edt_address = (EditText)findViewById(R.id.Edt_Address); 



bnt_Submit.setOnClickListener(new OnClickListener() { 


@Override 
     public void onClick(View v) { 
     // TODO Auto-generated method stub 

     password = edt_password.getText().toString(); 
     if (!isValidPassword(password)) { 
     edt_password.setError("The Passward must be at least 8 character "); 
     } 
     new RegisterUser().execute(); 



     } 
     }); 



} 


    private boolean isValidPassword(String pass) { 
    if (pass != null && pass.length() > 7) { 
    return true; 
    } 
    return false; 
    } 

    class RegisterUser extends AsyncTask<String, String, String> { 



    @Override 
    protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 

    pDialog = new ProgressDialog(AccountRegister.this); 
    pDialog.setMessage("Registering..."); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(true); 
    pDialog.show(); 


    } 

@Override 
protected String doInBackground(String... params) { 

// TODO Auto-generated method stub 

String name =edt_name.getText().toString(); 

String email =edt_email.getText().toString(); 
String phone =edt_phone.getText().toString(); 
String address =edt_address.getText().toString(); 




List<NameValuePair> params1 = new ArrayList<NameValuePair>(); 
params1.add(new BasicNameValuePair("name", name)); 
params1.add(new BasicNameValuePair("password", password)); 
params1.add(new BasicNameValuePair("email", email)); 
params1.add(new BasicNameValuePair("phone", phone)); 
params1.add(new BasicNameValuePair("address", address)); 



JSONObject json = jsonParser.makeHttpRequest(url_create_product, 
"POST", params1); 


Log.d("Create Response", json.toString()); 


try { 
int success = json.getInt(TAG_SUCCESS); 

if (success == 1) { 


} else if(success == 0) { 



JSONArray arr = new JSONArray("detail"); 

    for(int i = 0; i < arr.length(); i++){ 

      JSONObject c = arr.getJSONObject(i);   
      JSONArray ar_in = c.getJSONArray("name"); 

     for(int j = 0; j < ar_in.length(); j++){  
      Log.v("result--", ar_in.getString(j)); 
     } 
    } 





} 
} catch (JSONException e) { 
e.printStackTrace(); 
} 

return null; 

    } 

protected void onPostExecute(String file_url) { 
    // dismiss the dialog once done 

pDialog.dismiss(); 
} 

} 
} 
+0

你有什麼錯誤? – 2014-12-06 06:30:20

+0

我把它放到我的logcat中 {「success」:0,「detail」:{「phone」:[「電話字段是必需的。」],「address」:[「地址字段是必需的。 ],「email」:[「電子郵件字段是必需的。」],「密碼」:[「密碼字段是必需的。」],「名稱」:[「名稱字段是必需的。」]}} 但不知道如何在textview中顯示 – jayant 2014-12-06 06:45:03

+0

您是否嘗試過我的答案? – 2014-12-06 06:47:47

回答

0

如何轉換成的JSONObject 。例如"name":["The name field is required."]

您不需要將這些JSON數組轉換爲JSON對象。

當你發現"[]"意味着它的數組,而"{}"意味着它的對象。

在你的情況下,首先將你的總JSON轉換爲JSON對象,然後從其JSON數組中取回它的值。

JSONObject jsonObject = json.getJSONObject("detail"); 

    JSONArray jsonArray = c.getJSONArray("name"); 

    //you dont need to use for loop you have only one value in the array. 
     Log.v("Name is", jsonArray.getString(0)); 
    //like this change the array name and retrieve other array values. 
    } 

試試這個。希望這會幫助你。 :)

+0

感謝您的回覆,但我想獲得所有陣列名稱密碼電子郵件電話地址 – jayant 2014-12-06 07:04:22

+0

對於密碼嘗試此JSONArray jsonArray_password = c.getJSONArray(「password」);然後用電子郵件試試這個JSONArray jsonArray_email = c.getJSONArray(「email」);像這樣的其他人 – Biswajit 2014-12-06 07:06:30

+0

j正在做什麼,因爲我得到錯誤這是我如何寫代碼 JSONObject c = json.getJSONObject(「detail」); \t \t \t JSONArray jsonArray = c.getJSONArray(「name」); (「Name is」,jsonArray.getString(j)); – jayant 2014-12-06 07:24:47

0

您的JSON必須是這樣的:

{ 
"detail": [ 
    { 
     "name": "The name field is required.", 
     "password": "The password field is required.", 
     "email": "The email field is required.", 
     "phone": "The phone field is required.", 
     "address": "The address field is required." 
    }, 
    { 
     "name": "The name field is required.", 
     "password": "The password field is required.", 
     "email": "The email field is required.", 
     "phone": "The phone field is required.", 
     "address": "The address field is required." 
    } 
], 
"success": 0 
} 
0

嘗試這樣的:

JSONObject c = json.getJSONObject("detail"); 

    JSONArray ar_in = c.getJSONArray("name"); 

    for(int j = 0; j < ar_in.length(); j++){  
     Log.v("result--", ar_in.getString(j)); 
    } 
0
String detail = "{\"detail\":{\"name\":[\"The name field is required.\"],\"password\":[\"The password field is required.\"]," + 
     "\"email\":[\"The email field is required.\"]," + 
     "\"phone\":[\"The phone field is required.\"]," + 
     "\"address\":[\"The address field is required.\"]},\"success\":0}"; 

    JSONObject contentObj = new JSONObject(detail); 
    JSONObject detObj = contentObj.optJSONObject("detail"); 
    if (detObj != null) { 
    //if you need directly name 
     JSONArray nameArr = detObj.optJSONArray("name"); 
     String nameStr = nameArr.optString(0, ""); 

    //or iterate by all keys 
     Iterator<String> keys = detObj.keys(); 
     while (keys.hasNext()) { 
      JSONArray errorsArr = detObj.optJSONArray(keys.next()); 
      String errorStr = nameArr.optString(0, ""); 
     } 
    }