如何访问这样的排球响应

时间:2017-05-22 09:55:58

标签: android android-volley

如何像这样访问齐射响应{"data":"success"}

我已经尝试过这个

                    JSONObject j = null;
                    try {

                        j = new JSONObject(response);

                        result = j.getJSONArray("data");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

5 个答案:

答案 0 :(得分:1)

您尝试从JSONArray访问JSONObject,而它只是一个字符串。所以只需将getJSONArray替换为getString并尝试此

String data = new JSONObject(response).getString("data");

或在您的代码中

try {
    JSONObject j = new JSONObject(response);
    result = j.getString("data");
} 
catch (JSONException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

像这样访问

 JSONObject j = null;
                try {

                    j = new JSONObject(response);

                    result = j.getString("data");

                } catch (JSONException e) {
                    e.printStackTrace();
                }

答案 2 :(得分:0)

在检索该标记(即您的情况下的“数据”)是否存在于该json对象之前,您可以检查这一点。

import numpy as np
from scipy import optimize as opt

def f(x):
    return np.sin(x / 5.) * np.exp(x / 10.) + 5 * np.exp( -x / 2.)

aprox_0 = np.array([range(1, 10), range(11, 20), range(21, 30)])
min_0 = opt.minimize(f, aprox_0[0])

答案 3 :(得分:0)

由于data的值为string且价值为string,您必须使用JSONObject.getString("data")

使用:

result = j.getString("data");

代替:

result = j.getJSONArray("data");

答案 4 :(得分:-1)

“data”不是jsonarray,1#是正确的