Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 用方括号解析json_Android_Json_Parsing - Fatal编程技术网

Android 用方括号解析json

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(

我试图解析一个json,但我不知道如何解析这样的东西

{“最大电流”:[“100”,“25”]} -我想得到最大值(100)和当前值(25),请帮助我,谢谢。

试试这个:

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),您可以发布您的代码,以便轻松解决。