如何在 android 中将 JSONObject 转换为字符串数组或 ArrayList<String>
How to convert a JSONObject to a String Array or ArrayList<String> in android
我将获取 JSONObject 的值并将其放入字符串数组或 ArrayList 中。问题是我有一个 JSONObject 而不是 JSONArray,而且我无法更改 Web 服务。
这是我的 JSONObject:
{"THR":"تهران",
"MHD":"مشهد",
"KIH":"کیش",
"IFN":"اain",
"SYZ":"Paintage",
"TBZ":"init",
"ABD":"آبادان"}
这是我的代码:
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
progressbar.setVisibility(View.VISIBLE);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), "Response", Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.GONE);
try {
JSONArray array = new JSONArray(response);
Toast.makeText(getApplicationContext(), "Try", Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Catch", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.GONE);
}
});
queue.add(stringRequest);
当我调试我的代码时,它进入了 CATCH!!!
无论如何,它不会去尝试!
我想要这样的东西:
String[] strArray = {"تهران","مشهد","کیش","اصفهان","شیراز","تبریز","آبادان"};
如何获取?
public static void main(String[] args) {
//building your jsonObject(took only the 1st four ;P )
JSONObject objJsonObject = new JSONObject();
objJsonObject.put("THR", "تهران");
objJsonObject.put("MHD", "مشهد");
objJsonObject.put("KIH", "کیش");
objJsonObject.put("IFN", "اصفهان");
String [] strArray= new String[objJsonObject.length()];
int i = 0;
//adding your required data into a array
for (String key: objJsonObject.keySet()) {
strArray[i] = objJsonObject.getString(key);
i++;
}
//printing your data
for (int j = 0; j < strArray.length; j++) {
System.out.println(strArray[j]);
}
}
希望这对您有所帮助....
对您的代码进行了更改
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
ArrayList<String> answersStringArray= new ArrayList<String>(); // define string array
progressbar.setVisibility(View.VISIBLE);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), "Response", Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.GONE);
try {
JJSONObject jsonObject = new JSONObject(response); //cast responce to jsonObject
Iterator<?> keys = jsonObject.keys(); // get the keys of the jsonObject
while( keys.hasNext() ) {//iterrate over them
String key = (String)keys.next();
answersStringArray.add(jsonObject.optString(key));}// add the string to our array of stings
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Catch", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.GONE);
}
});
queue.add(stringRequest);
我将获取 JSONObject 的值并将其放入字符串数组或 ArrayList 中。问题是我有一个 JSONObject 而不是 JSONArray,而且我无法更改 Web 服务。 这是我的 JSONObject:
{"THR":"تهران", "MHD":"مشهد", "KIH":"کیش", "IFN":"اain", "SYZ":"Paintage", "TBZ":"init", "ABD":"آبادان"}
这是我的代码:
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
progressbar.setVisibility(View.VISIBLE);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), "Response", Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.GONE);
try {
JSONArray array = new JSONArray(response);
Toast.makeText(getApplicationContext(), "Try", Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Catch", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.GONE);
}
});
queue.add(stringRequest);
当我调试我的代码时,它进入了 CATCH!!! 无论如何,它不会去尝试!
我想要这样的东西:
String[] strArray = {"تهران","مشهد","کیش","اصفهان","شیراز","تبریز","آبادان"};
如何获取?
public static void main(String[] args) {
//building your jsonObject(took only the 1st four ;P )
JSONObject objJsonObject = new JSONObject();
objJsonObject.put("THR", "تهران");
objJsonObject.put("MHD", "مشهد");
objJsonObject.put("KIH", "کیش");
objJsonObject.put("IFN", "اصفهان");
String [] strArray= new String[objJsonObject.length()];
int i = 0;
//adding your required data into a array
for (String key: objJsonObject.keySet()) {
strArray[i] = objJsonObject.getString(key);
i++;
}
//printing your data
for (int j = 0; j < strArray.length; j++) {
System.out.println(strArray[j]);
}
}
希望这对您有所帮助....
对您的代码进行了更改
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
ArrayList<String> answersStringArray= new ArrayList<String>(); // define string array
progressbar.setVisibility(View.VISIBLE);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), "Response", Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.GONE);
try {
JJSONObject jsonObject = new JSONObject(response); //cast responce to jsonObject
Iterator<?> keys = jsonObject.keys(); // get the keys of the jsonObject
while( keys.hasNext() ) {//iterrate over them
String key = (String)keys.next();
answersStringArray.add(jsonObject.optString(key));}// add the string to our array of stings
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Catch", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
progressbar.setVisibility(View.GONE);
}
});
queue.add(stringRequest);