类型 JSONArray 无法转换为 JSONObject
Type JSONArray cannot be converted to JSONObject
当我调用我的服务网站时,我遇到了这个异常:类型 org.json.JSONArray
的 org.json.JSONException:Value[{}]
无法转换为 JSONObject
private void jsonParse(){
String url="http://192.168.56.1:8095/rest/workouts/all";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("");
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject w = jsonArray.getJSONObject(i);
String title = w.getString("title");
String goal = w.getString("goal");
txtv.append(title + ", " + goal +"\n\n");
}
...
顺便说一句,这是 JSON
格式
[{"id":"1","title":"title","goal":"goal","exercice":"Exercice
1","difficulty":"Beginner","duration":"3","image":"..","description":"..."},]
您的 json 响应实际上是一个 JsonArray,因此您需要将代码更改为:
private void jsonParse(){
String url="http://192.168.56.1:8095/rest/workouts/all";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray >() {
@Override
public void onResponse(JSONArray response) {
try {
for(int i = 0; i < response.length(); i++) {
JSONObject w = response.getJSONObject(i);
String title = w.getString("title");
String goal = w.getString("goal");
txtv.append(title + ", " + goal +"\n\n");
}
尝试
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i = 0; i < response.length(); i++) {
JSONObject w = response.getJSONObject(i);
String title = w.getString("title");
String goal = w.getString("goal");
txtv.append(title + ", " + goal +"\n\n");
}
}catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
// Do something when error occurred
}
}
);
虽然答案可以处理你的情况,但看看你的 JSON,它显然是一个数组而不是对象,你的 JSON 的正确格式可能是这样的:
{"id":"1","title":"title","goal":"goal","exercice":"Exercice1","difficulty":"Beginner","duration":"3","image":"..","description":"..."}
其中没有“[”、“]”和“,”。所以这是我的解决方案,如果它一直只接收一个对象,你应该改变你的API,只发送一个对象,就像我上面提到的那样,否则,最后的“,”是不正确的一个只有一个项目的数组,无论如何,您必须将模型更改为 except 数组 [您可以将其声明为列表,如:List<Model> myModelList
] 以便解析器正常工作。
试试这个
try{
JSONArray jsonArray = new JSONArray(response.string());
for (int i=0;i<jsonArray.length();i++){
String Title=jsonArray.getJSONObject(i).getString("title");
String Goal=jsonArray.getJSONObject(i).getString("goal");
}
}
catch (Exception e){
e.printStackTrace();
}
试试这个……应该是
JSONArray jsonarray = new JSONArray(response);
然后是
for(int i=0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
//your code
}
这是我找到的解决方案,谢谢大家♥..
private void jsonParse(){
String url="http://192.168.56.1:8095/rest/workouts/all";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,url,null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
try {
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String title = jsonobject.getString("title");
String goal = jsonobject.getString("goal");
txtv.append(title + ", " + goal +"\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
mQueue.add(jsonArrayRequest);
}
当我调用我的服务网站时,我遇到了这个异常:类型 org.json.JSONArray
的 org.json.JSONException:Value[{}]
无法转换为 JSONObject
private void jsonParse(){
String url="http://192.168.56.1:8095/rest/workouts/all";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("");
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject w = jsonArray.getJSONObject(i);
String title = w.getString("title");
String goal = w.getString("goal");
txtv.append(title + ", " + goal +"\n\n");
}
...
顺便说一句,这是 JSON
格式
[{"id":"1","title":"title","goal":"goal","exercice":"Exercice
1","difficulty":"Beginner","duration":"3","image":"..","description":"..."},]
您的 json 响应实际上是一个 JsonArray,因此您需要将代码更改为:
private void jsonParse(){
String url="http://192.168.56.1:8095/rest/workouts/all";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray >() {
@Override
public void onResponse(JSONArray response) {
try {
for(int i = 0; i < response.length(); i++) {
JSONObject w = response.getJSONObject(i);
String title = w.getString("title");
String goal = w.getString("goal");
txtv.append(title + ", " + goal +"\n\n");
}
尝试
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i = 0; i < response.length(); i++) {
JSONObject w = response.getJSONObject(i);
String title = w.getString("title");
String goal = w.getString("goal");
txtv.append(title + ", " + goal +"\n\n");
}
}catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
// Do something when error occurred
}
}
);
虽然答案可以处理你的情况,但看看你的 JSON,它显然是一个数组而不是对象,你的 JSON 的正确格式可能是这样的:
{"id":"1","title":"title","goal":"goal","exercice":"Exercice1","difficulty":"Beginner","duration":"3","image":"..","description":"..."}
其中没有“[”、“]”和“,”。所以这是我的解决方案,如果它一直只接收一个对象,你应该改变你的API,只发送一个对象,就像我上面提到的那样,否则,最后的“,”是不正确的一个只有一个项目的数组,无论如何,您必须将模型更改为 except 数组 [您可以将其声明为列表,如:List<Model> myModelList
] 以便解析器正常工作。
试试这个
try{
JSONArray jsonArray = new JSONArray(response.string());
for (int i=0;i<jsonArray.length();i++){
String Title=jsonArray.getJSONObject(i).getString("title");
String Goal=jsonArray.getJSONObject(i).getString("goal");
}
}
catch (Exception e){
e.printStackTrace();
}
试试这个……应该是
JSONArray jsonarray = new JSONArray(response);
然后是
for(int i=0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
//your code
}
这是我找到的解决方案,谢谢大家♥..
private void jsonParse(){
String url="http://192.168.56.1:8095/rest/workouts/all";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,url,null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
try {
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String title = jsonobject.getString("title");
String goal = jsonobject.getString("goal");
txtv.append(title + ", " + goal +"\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
mQueue.add(jsonArrayRequest);
}