有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

无法将java JSONObject转换为字符串

我在堆栈中多次看到这个问题,但运气不好。问题是,我正在使用这个API验证我的CNPJ字段,如果我有连接,响应将是字段“nome”,并填充我的textview字段

到目前为止,JSON是有效的(已经在jsonformatter中传递),但我无法通过JSONArray找到对象,当我设法通过JSONObject找到它时,它告诉我不能转换为字符串

 valide.setOnClickListener(view1 -> {
        //String PJ = cnpj.getText().toString();
        String PJ = "06990590000123";
        String url = "https://www.receitaws.com.br/v1/cnpj/" + PJ;
        jsonParse(url);
    });


private void jsonParse(String url) {
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

        String json;
        @Override
        public void onResponse(JSONObject response) {
            try {
                json = response.getJSONObject("nome").toString();

                razao.append(json);
                razao.setText(json);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), "Invalid ! ", Toast.LENGTH_SHORT).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError erro) {
            erro.printStackTrace();
        }
    });

    mQueue.add(request); //Volley.newRequestQueue
}

JSON

{ "atividade_principal": [ { "text": "Portais, provedores de conteúdo e outros serviços de informação na internet", "code": "63.19-4-00" } ],

"data_situacao": "01/09/2004",

"complemento": "ANDAR 17A20 TSUL 2 17A20", "tipo": "MATRIZ",

**"nome": "GOOGLE BRASIL INTERNET LTDA.", //Need this field**

"uf": "SP",

"telefone": "(11) 2395-8400",

"email": "googlebrasil@google.com",

日志

org.json.JSONException: Value GOOGLE BRASIL INTERNET LTDA. at nome of type java.lang.String cannot be converted to JSONObject at org.json.JSON.typeMismatch(JSON.java:101)

使用的URL

https://www.receitaws.com.br/v1/cnpj/06990590000123

有人能帮我解决这个问题吗?谢谢大家!


共 (2) 个答案

  1. # 1 楼答案

    在JSON中nome是字符串类型。因此,不要使用来自JSONObject类的getJSONObject方法。因此,您的代码应该如下所示:

    private void jsonParse(String url) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
    
            String json;
            @Override
            public void onResponse(JSONObject response) {
                try {
                    json = response.getString("nome"); // Here is the change
    
                    razao.append(json);
                    razao.setText(json);
    
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getActivity(), "Invalid ! ", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError erro) {
                erro.printStackTrace();
            }
        });
    
        mQueue.add(request); //Volley.newRequestQueue
    }
    
  2. # 2 楼答案

    试试这个: 首先构造JsonObject,然后获取键的字符串值

    JSONObject jsonObject = new JSONObject(json);
    String valueIWanted = jsonObject.getString("nome"))