How can I parse in Android a Json array of strings and save it in a java string array ( like: xy[ ] ) ?
如何在Android中解析Json字符串数组并将其保存到java字符串数组中(比如:xy[]) ?
My Json to be parsed :
解析我的Json:
[
{
"streets": [ "street1", "street2", "street3",... ],
}
]
Later in my code I want to populated with that array a spinner item in my layout. Everything i tried enden with only one street item listed in the spinner.
在后面的代码中,我希望在布局中使用该数组填充一个微调项。我所做的每件事都只有一个在转盘里的街道项目。
3 个解决方案
#1
33
To parse
解析
try {
JSONArray jr = new JSONArray("Your json string");
JSONObject jb = (JSONObject)jr.getJSONObject(0);
JSONArray st = jb.getJSONArray("streets");
for(int i=0;i<st.length();i++)
{
String street = st.getString(i);
Log.i("..........",""+street);
// loop and add it to array or arraylist
}
}catch(Exception e)
{
e.printStackTrace();
}
Once you parse and add it to array. Use the same to populate your spinner.
一旦解析并将其添加到数组中。使用相同的方法填充微调控制项。
[
represents json array node
表示json数组节点。
{
represents json object node
{表示json对象节点
#2
3
Try this..
试试这个. .
JSONArray arr = new JSONArray(json string);
for(int i = 0; i < arr.length(); i++){
JSONObject c = arr.getJSONObject(i);
JSONArray ar_in = c.getJSONArray("streets");
for(int j = 0; j < ar_in.length(); j++){
Log.v("result--", ar_in.getString(j));
}
}
#3
2
We need to make JSON object first. For example,
我们需要先创建JSON对象。例如,
JSONObject jsonObject = new JSONObject(resp);
// resp is your JSON string
JSONArray arr = jsonObject.getJSONArray("results");
Log.i(LOG, "arr length = " + arr.length());
for(int i=0;i<arr.length();i++)
{...
arr may contains other JSON Objects or JSON array. How to convert the JSON depends on the String. There is a complete example with some explanation about JSON String to JSON array can be found at http://www.hemelix.com/JSONHandling
arr可以包含其他JSON对象或JSON数组。如何转换JSON取决于字符串。在http://www.hemelix.com/JSONHandling可以找到关于JSON字符串到JSON数组的完整示例
#1
33
To parse
解析
try {
JSONArray jr = new JSONArray("Your json string");
JSONObject jb = (JSONObject)jr.getJSONObject(0);
JSONArray st = jb.getJSONArray("streets");
for(int i=0;i<st.length();i++)
{
String street = st.getString(i);
Log.i("..........",""+street);
// loop and add it to array or arraylist
}
}catch(Exception e)
{
e.printStackTrace();
}
Once you parse and add it to array. Use the same to populate your spinner.
一旦解析并将其添加到数组中。使用相同的方法填充微调控制项。
[
represents json array node
表示json数组节点。
{
represents json object node
{表示json对象节点
#2
3
Try this..
试试这个. .
JSONArray arr = new JSONArray(json string);
for(int i = 0; i < arr.length(); i++){
JSONObject c = arr.getJSONObject(i);
JSONArray ar_in = c.getJSONArray("streets");
for(int j = 0; j < ar_in.length(); j++){
Log.v("result--", ar_in.getString(j));
}
}
#3
2
We need to make JSON object first. For example,
我们需要先创建JSON对象。例如,
JSONObject jsonObject = new JSONObject(resp);
// resp is your JSON string
JSONArray arr = jsonObject.getJSONArray("results");
Log.i(LOG, "arr length = " + arr.length());
for(int i=0;i<arr.length();i++)
{...
arr may contains other JSON Objects or JSON array. How to convert the JSON depends on the String. There is a complete example with some explanation about JSON String to JSON array can be found at http://www.hemelix.com/JSONHandling
arr可以包含其他JSON对象或JSON数组。如何转换JSON取决于字符串。在http://www.hemelix.com/JSONHandling可以找到关于JSON字符串到JSON数组的完整示例