如何从Java语言的JSON响应中提取键值?我想提取"doj"的值,但我无法这样做


{
    "reservation_upto": {
        "lng": 78.0098161,
        "lat": 27.1752554,
        "code": "AGC",
        "name": "AGRA CANTT"
    },
    "debit": 3,
    "doj": "28-05-2018",
    "to_station": {
        "lng": 78.0098161,
        "lat": 27.1752554,
        "code": "AGC",
        "name": "AGRA CANTT"
    },
    "response_code": 200,
    "boarding_point": {
        "lng": 80.2755685,
        "lat": 13.081674,
        "code": "MAS",
        "name": "CHENNAI CENTRAL"
    },
    "pnr": "4405474586",
    "chart_prepared": false,
    "journey_class": {
        "code": "3A",
        "name": null
    },

这是我尝试使用凌空

尝试的
private void loaddata() {
    String url = "https://api.railwayapi.com/v2/pnr-status/pnr/4655474586/apikey/q15rfl3kpz/";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, 
         new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        progressDialog.dismiss();
                        JSONObject obj = null;
                        try {
                            String str="";
                            obj = response.getJSONObject("name");
                            str = String.valueOf(obj);
                            TextView tv = (TextView)findViewById(R.id.textView);
                            tv.append(str);
                        }
                    }
                }
            }
        }
JSONObject obj = (JSONObject) object;    
String doj = (String) obj.get("doj")

您可以使用JSONOBJECT obj来找出密钥doj的值。

后者,如果您想将DOJ作为其他数据类型,则可以在其中解析。可能这可能会有所帮助。我对凌空没有太多想法:(

最新更新