Probably a stupid question. I'm getting a JSONArray in the form of
可能是一个愚蠢的问题。我正在以一种形式获得JSONArray
[{'route':'route1'}, {'route':'route2'}, {'route':'route3'}]
I want to get it into a String array of
我想把它变成一个String数组
["route1", "route2", "route3"]
How?
怎么样?
6 个解决方案
#1
6
The solution that comes to my mind would be to iterate through and just grab the values
我想到的解决方案是迭代并获取值
String[] stringArray = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
stringArray[i]= jsonArray.getJSONObject(i).getString("route");
}
#2
2
Try this
尝试这个
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(responseString);
if (jsonArray != null) {
String[] strArray = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
strArray[i] = jsonArray.getJSONObject(i).getString("route");
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
#3
0
Followed by this TUTORIAL
接下来是这个教程
JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(i = 0; i < arr.length; i++){
list.add(arr.getJSONObject(i).getString("name"));
}
Or use GSON
或者使用GSON
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
//(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]
//(Deserialization)
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
//ints2 is same as ints
#4
0
You can try below solutions,
您可以尝试以下解决方案,
Just replace { and } by following code
只需通过以下代码替换{和}即可
String jsonString = jsonArray.toString();
jsonString.replace("},{", " ,");
String[]array = jsonString.split(" ");
Or
要么
JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(i = 0; i < arr.length; i++){
list.add(arr.getJSONObject(i).getString("name"));
}
this will convert to Arraylist and then if you want it to string then convert it to StringArray. for more reference use this link
这将转换为Arraylist然后如果你想要它的字符串然后将其转换为StringArray。有关更多参考,请使用此链接
#5
0
as straight forward as it can be
尽可能直截了当
List<String> list = new ArrayList<String>();
for( int ix = 0; ix < yourArray.length(); ix++ ){
list.add( yourArray.getJSONObject( ix ).getString( "route" ) );
}
return list.toArray( new String[] );
#6
0
// try this way here i gave with demo code
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("route","route1");
jsonArray.put(jsonObject1);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("route","route2");
jsonArray.put(jsonObject2);
JSONObject jsonObject3 = new JSONObject();
jsonObject3.put("route","route3");
jsonArray.put(jsonObject3);
String[] array = jsonArrayToArray(jsonArray);
for (int i=0;i<array.length;i++){
Log.i((i+1)+" Route : ",array[i]);
}
}catch (Exception e){
e.printStackTrace();
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public String jsonToStrings(JSONObject object) throws JSONException {
String data ="";
Iterator keys = object.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
data+=fromJson(object.get(key)).toString()+",";
}
return data;
}
private Object fromJson(Object json) throws JSONException {
if (json == JSONObject.NULL) {
return null;
} else if (json instanceof JSONObject) {
return jsonToStrings((JSONObject) json);
} else if (json instanceof JSONArray) {
return jsonArrayToArray((JSONArray) json);
} else {
return json;
}
}
private String[] jsonArrayToArray(JSONArray array) throws JSONException {
ArrayList<Object> list = new ArrayList<Object>();
int size = array.length();
for (int i = 0; i < size; i++) {
list.add(fromJson(array.get(i)));
}
ArrayList<String> arrayList = new ArrayList<String>();
for (int i=0;i<list.size();i++){
String[] row = ((String)((String)list.get(i)).subSequence(0,((String)list.get(i)).length()-1)).split(",");
for (int j=0;j<row.length;j++){
arrayList.add(row[j]);
}
}
String[] strings = new String[arrayList.size()];
for (int k=0;k<strings.length;k++){
strings[k]=arrayList.get(k);
}
return strings;
}
}
#1
6
The solution that comes to my mind would be to iterate through and just grab the values
我想到的解决方案是迭代并获取值
String[] stringArray = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
stringArray[i]= jsonArray.getJSONObject(i).getString("route");
}
#2
2
Try this
尝试这个
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(responseString);
if (jsonArray != null) {
String[] strArray = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
strArray[i] = jsonArray.getJSONObject(i).getString("route");
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
#3
0
Followed by this TUTORIAL
接下来是这个教程
JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(i = 0; i < arr.length; i++){
list.add(arr.getJSONObject(i).getString("name"));
}
Or use GSON
或者使用GSON
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
//(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]
//(Deserialization)
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
//ints2 is same as ints
#4
0
You can try below solutions,
您可以尝试以下解决方案,
Just replace { and } by following code
只需通过以下代码替换{和}即可
String jsonString = jsonArray.toString();
jsonString.replace("},{", " ,");
String[]array = jsonString.split(" ");
Or
要么
JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(i = 0; i < arr.length; i++){
list.add(arr.getJSONObject(i).getString("name"));
}
this will convert to Arraylist and then if you want it to string then convert it to StringArray. for more reference use this link
这将转换为Arraylist然后如果你想要它的字符串然后将其转换为StringArray。有关更多参考,请使用此链接
#5
0
as straight forward as it can be
尽可能直截了当
List<String> list = new ArrayList<String>();
for( int ix = 0; ix < yourArray.length(); ix++ ){
list.add( yourArray.getJSONObject( ix ).getString( "route" ) );
}
return list.toArray( new String[] );
#6
0
// try this way here i gave with demo code
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("route","route1");
jsonArray.put(jsonObject1);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("route","route2");
jsonArray.put(jsonObject2);
JSONObject jsonObject3 = new JSONObject();
jsonObject3.put("route","route3");
jsonArray.put(jsonObject3);
String[] array = jsonArrayToArray(jsonArray);
for (int i=0;i<array.length;i++){
Log.i((i+1)+" Route : ",array[i]);
}
}catch (Exception e){
e.printStackTrace();
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public String jsonToStrings(JSONObject object) throws JSONException {
String data ="";
Iterator keys = object.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
data+=fromJson(object.get(key)).toString()+",";
}
return data;
}
private Object fromJson(Object json) throws JSONException {
if (json == JSONObject.NULL) {
return null;
} else if (json instanceof JSONObject) {
return jsonToStrings((JSONObject) json);
} else if (json instanceof JSONArray) {
return jsonArrayToArray((JSONArray) json);
} else {
return json;
}
}
private String[] jsonArrayToArray(JSONArray array) throws JSONException {
ArrayList<Object> list = new ArrayList<Object>();
int size = array.length();
for (int i = 0; i < size; i++) {
list.add(fromJson(array.get(i)));
}
ArrayList<String> arrayList = new ArrayList<String>();
for (int i=0;i<list.size();i++){
String[] row = ((String)((String)list.get(i)).subSequence(0,((String)list.get(i)).length()-1)).split(",");
for (int j=0;j<row.length;j++){
arrayList.add(row[j]);
}
}
String[] strings = new String[arrayList.size()];
for (int k=0;k<strings.length;k++){
strings[k]=arrayList.get(k);
}
return strings;
}
}