Android 用方括号解析json
我试图解析一个json,但我不知道如何解析这样的东西 {“最大电流”:[“100”,“25”]} -我想得到最大值(100)和当前值(25),请帮助我,谢谢。试试这个:Android 用方括号解析json,android,json,parsing,Android,Json,Parsing,我试图解析一个json,但我不知道如何解析这样的东西 {“最大电流”:[“100”,“25”]} -我想得到最大值(100)和当前值(25),请帮助我,谢谢。试试这个: String jsonString = {"max_current":["100","25"]}; try { JSONObject jsonObj = new JSONObject(JsonString); JSONArray jsonArray = jsonObj.getJSONArray(
String jsonString = {"max_current":["100","25"]};
try {
JSONObject jsonObj = new JSONObject(JsonString);
JSONArray jsonArray = jsonObj.getJSONArray("max_current");
int max = jsonArray.getInt(0);
int currnet = jsonArray.getInt(1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
使用以下命令:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
yourwebserivce_Url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
JSONArray arr= response.getJSONArray("max_current");
for(int i=0;i<arr.length();i++)
{
JSONObject g=arr.getJSONObject(i);
int max = g.getInt(0);
int currnet = g.getInt(1);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
// hide the progress dialog
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.GET,
yourwebserivce_Url,空,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(TAG,response.toString());
JSONArray arr=response.getJSONArray(“最大电流”);
对于(inti=0;i@JAC),您可以发布您的代码,以便轻松解决。