这是我的 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 :(得分:0)
{
"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
}
答案 1 :(得分: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));
}
答案 2 :(得分:0)
如何将其转换为jsonobject。例如"name":["The name field is required."]
您不需要将这些JSONarrays转换为JSONobjects。
当您发现"[]"
表示其数组时,"{}"
表示其对象。
在您的情况下,首先将您的总json转换为JSONobject,然后从其JSONarray中检索其值。
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.
}
试试这个。希望这会帮助你。 :)
答案 3 :(得分: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, "");
}
}