简体   繁体   English

JSON RPC对Array Android的响应

[英]JSON RPC response to Array Android

I managed to get results for a json rpc request, like this: 我设法获得了json rpc请求的结果,如下所示:

{"id":1,"jsonrpc":"2.0","result":{"channels":[{"channel":"Channel A ","channelid":2165,"label":"Channel A"},{"channel":"Channel B ","channelid":748,"label":"Channel B"} etc. {“ id”:1,“ jsonrpc”:“ 2.0”,“结果”:{“ channels”:[{“ channel”:“ Channel A”,“ channelid”:2165,“ label”:“ Channel A”} ,{“ channel”:“ Channel B”,“ channelid”:748,“ label”:“ Channel B”}等。

I would like to parse the reponse to an array where i can read channel,channelid and label at each iteration of a loop. 我想将响应解析为一个数组,在该数组中,我可以在每次循环迭代时读取channel,channelid和label。 but did not manage to get it. 但没有成功。

Any help would be much appreciated. 任何帮助将非常感激。

Sorry not that experienced with programming , its al self-taught 很抱歉,编程经验不足,这是自学成才的

Code for json rpc request : json rpc请求的代码:

  String groupchannelsurl = "http://192.168.2.100:8080/jsonrpc?request={\\"jsonrpc\\": \\"2.0\\", \\"method\\": \\"PVR.GetChannels\\", \\"params\\": {\\"channelgroupid\\" : 9,\\"properties\\":[\\"channel\\"]}, \\"id\\": 1 }"; RequestQueue queue = Volley.newRequestQueue(this); JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, groupchannelsurl, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // display response Log.d("Response", response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("Error.Response", error.toString()); } } ); queue.add(getRequest); 

The Android Developer documentation is really good if you get stuck. 如果遇到困难,Android Developer文档非常好。

JSONObject - https://developer.android.com/reference/org/json/JSONObject.html JSONObject- https://developer.android.com/reference/org/json/JSONObject.html

JSONArray - https://developer.android.com/reference/org/json/JSONArray.html JSONArray- https://developer.android.com/reference/org/json/JSONArray.html

@Override
public void onResponse(JSONObject response) {
    JSONArray channels = response.getJSONObject("result").getJSONArray("channels");
    for (int i = 0; i < channels.length(); i++) {
        JSONObject channel = channels.getJSONObject(i);
        String channelName = channel.getString("channel");
        int channelId = channel.getInt("channelid");
        String channelLabel = channel.getString("label");
        // ...
    }
}

Hope this helps! 希望这可以帮助!

You can parse JSONObject like below 您可以如下解析JSONObject

private void parseJsonObject(JSONObject responseObject) throws JSONException {

    String id = responseObject.getString("id");

    JSONArray jsonArray = responseObject.optJSONArray("channels");
    if (jsonArray != null) {

        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject arrayObject = (JSONObject) jsonArray.get(i);

            String channel = arrayObject.getString("channel");
            String channelId = arrayObject.getString("channelid");


        }


    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM