-2
[
{
    "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 ));
        }

我使用了这段代码但没有得到它......请帮助我

4

2 回答 2

3

您可以参考下面的代码对于 json

[ 
{
  "name" : "Test",
  "id" : 512
}, {
  "name" : "Test2",
  "id" : 573
}, {
  "name" : "Test3",
  "id" : 585
}
]

像这样解析

    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);
于 2013-06-20T07:29:48.423 回答
0

您好,请在此处找到针对其 asmx 服务问题的示例代码,并从那里获取 json 作为输出:这是代码,请相应地放置您的服务网址:https://skydrive.live.com/redir?resid=8FBD3A73D6D8BFC1! 233&authkey=!AKVwa6BLk0nF5sU

于 2013-06-20T08:59:13.893 回答