我在Stack中看到了這個問題,但沒有任何收獲。問題是,我正在使用這個 API 來驗證我的 CNPJ 欄位,如果我有一個連接,回應將是欄位 "nome "并填充我的 textview 欄位。
到目前為止還不錯,JSON是有效的(已經在jsonformatter中傳遞),但是我無法通過JSONArray找到物件,當我設法通過JSONObject找到它時,它告訴我不能轉換為String。
valide.setOnClickListener(view1 -> {
//String PJ = cnpj.getText().toString();
String PJ = "06990590000123"/span>。
String url = "https://www.receitaws.com.br/v1/cnpj/"/span> PJ;
jsonParse(url)。
});
private void jsonParse(String url) {
JsonObjectRequest request = new JsonObjectRequest(Request. Method.GET, url, null, new Response.Listener<JSONObject> () {
字串json。
@Override
public void onResponse(JSONObject response) {
try {
json = response.getJSONObject("nome"/span>).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
{ "antividade_principal": [ { "text": "Portais, provedores de conteúdo e outros services de informa??o na internet", "code": "63.19-4-00" }. ],
"data_situacao": "01/09/2004",
"complemento": "ANDAR 17A20 TSUL 2 17A20", "提示": "MATRIZ",
**"nome": "GOOGLE BRASIL INTERNET LTDA.", //Need this field**.
"uf"。"SP",
"telefone": "(11)2395-8400"。
"email": "[email protected]"。
LOG
org.json.JSONException: 值GOOGLE BRASIL INTERNET LTDA.在nome of 型別的java.lang.String不能被轉換為JSONObject。 在org.json.JSON.typeMismatch(JSON.java:101)
。
使用的URL
https://www.receitaws.com.br/v1/cnpj/06990590000123
誰能幫助我解決這個問題,請?謝謝你!
uj5u.com熱心網友回復:
在你的JSON中,nome的型別是String。所以不要使用getJSONObject,而是使用getString方法,該方法來自JSONObject類。所以你的代碼應該如下:
private void jsonParse(字串url) {
JsonObjectRequest request = new JsonObjectRequest(Request. Method.GET, url, null, new Response.Listener<JSONObject> () {
字串json。
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() {
public void onErrorResponse(VolleyError erro) {
erro.printStackTrace()。
}
});
mQueue.add(request); //Volley.newRequestQueue。
}
uj5u.com熱心網友回復:
試試這個。 首先構造JsonObject,然后得到鍵的字串值。
JSONObject jsonObject = new JSONObject(json)。
String valueIWanted = jsonObject.getString("nome"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/331555.html
標籤:
上一篇:如何從一個陣串列中計算長度
