[
{
"value": "--NA--",
"id": "251"
},
{
"value": "Adilabad",
"id": "1346"
},
{
"value": "Yavatmal",
"id": "1478"
}
]
如何从json webservice解析json上面的android spinner。
我也经历了很多解决方案,但没有到达那里。 我试过这个,但它不适合我
public class SpinnerDemo extends Activity {
//JSON node names
private static final String TAG_DATA = "data";
private static final String TAG_CREATED_AT = "created_at";
private static final String TAG_ID_DRINK = "id";
private static final String TAG_NAME = "name";
private static final String TAG_PRICE = "price";
private static final String TAG_UPDATED_AT = "updated_at";
private static final String MAP_API_URL = "http://domainname.com/webservice/WSHMS.asmx?op=GetCityList";
private BackGroundTask bgt;
Spinner drinkField;
ArrayList<Drink> drinkList = new ArrayList<Drink>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_main);
buildDrinkDropDown();
}
private void buildDrinkDropDown() {
List<NameValuePair> apiParams = new ArrayList<NameValuePair>(1);
apiParams.add(new BasicNameValuePair("call", "drinkList"));
bgt = new BackGroundTask(MAP_API_URL, "GET", apiParams);
try {
JSONObject drinkJSON = bgt.execute().get();
// Getting Array of drinks
JSONArray drinks = drinkJSON.getJSONArray(TAG_DATA);
// looping through All drinks
for (int i = 0; i < drinks.length(); i++) {
JSONObject d = drinks.getJSONObject(i);
// Storing each json item in variable
String id = d.getString(TAG_ID_DRINK);
String createdAt = d.getString(TAG_CREATED_AT);
String updatedAt = d.getString(TAG_UPDATED_AT);
String price = d.getString(TAG_PRICE);
String name = d.getString(TAG_NAME);
// add drink
drinkList.add(new Drink( createdAt ,id, name, price,updatedAt ));
}
我使用了这段代码,但没有得到它...... 请帮帮我
答案 0 :(得分:3)
您可以参考下面的代码 对于json
[
{
"name" : "Test",
"id" : 512
}, {
"name" : "Test2",
"id" : 573
}, {
"name" : "Test3",
"id" : 585
}
]
Parse喜欢这个
ArrayList<String> arrProducts = new ArrayList<String>();
try {
JSONArray valarray = new JSONArray(jsonstring);
for (int i = 0; i < valarray.length(); i++) {
String str = valarray.getJSONObject(i).getString("name");
arrProducts.add(str);
}
} catch (JSONException e) {
Log.e("JSON", "There was an error parsing the JSON", e);
}
然后你可以像这样设置微调器
ArrayAdapter<String> productAdapter = new ArrayAdapter<String>(
MyActivity.this,
android.R.layout.simple_spinner_item, arrProducts);
productAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
productSpinner.setAdapter(productAdapter);
答案 1 :(得分:0)
嗨请找到这里附带的示例代码,问题是asmx服务,并从那里获取json作为输出:这里是代码,plz相应地放置了你的服务URL:https://skydrive.live.com/redir?resid=8FBD3A73D6D8BFC1!233&authkey=!AKVwa6BLk0nF5sU