Android 使用Volley通过URL解析JSON
我试图解析我使用截取从URL接收的JSON数据。但它不能正常工作。我无法理解jasonArray和jasonObject之间的区别Android 使用Volley通过URL解析JSON,android,json,parsing,android-volley,Android,Json,Parsing,Android Volley,我试图解析我使用截取从URL接收的JSON数据。但它不能正常工作。我无法理解jasonArray和jasonObject之间的区别 public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private ExampleAdapter mExampleAdapter; private ArrayList<ExampleItem> mExa
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(this);
parseJSON();
}
private void parseJSON() {
String url = "http://api.visitkorea.or.kr/openapi/service/rest/KorService/locationBasedList?" +
"serviceKey=gCoROJjTpFwTjV%2F%2BoWBcWMdj0z%2Fxsu22eY19j%2FoeNSJOnrkaPehhoyIzp%2FrtMkNYAzVlBFzmnI6cCsKODNmejA%3D%3D&" +
"numOfRoews=10&pageNo=1&startPage=1&MobileOS=AND&MobileApp=WelcomeToSeoul&_type=json&arrange=A&contenTypeId=15&mapX=126.981611&mapY=37.568477&radius=1000&listYN=Y";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject parse_respone = response.getJSONObject("response");
JSONObject parse_body = parse_respone.getJSONObject("body");
JSONObject parse_items = parse_body.getJSONObject("items");
JSONArray jsonArray = parse_items.getJSONArray("item");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String creatorName = hit.getString("title");
String imageUrl = hit.getString("firstimage");
int likeCount = hit.getInt("siguncode");
mExampleList.add(new ExampleItem(imageUrl, creatorName, likeCount));
}
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
}
我搜索了很多,但无法解决问题。请帮我解决这个问题。您的代码逻辑基本正确。我已将您的代码复制到我的项目中并对其进行了测试。我发现了你的代码不起作用的原因 请查看下面的代码行
int likeCount = hit.getInt("siguncode");
json响应中没有字段siguncode
。相反,您有sigungucode
字段。这就是你的代码不起作用的原因
PS:JSONObject和JSONArray之间的区别
JSONObject只是一个具有键/值映射的对象
JSONArray是包含一个或多个JSONObject的集合
在JSON示例中,“header”
是JSONObject。
而“items”
中的“item”
是JSONArray
编辑
为了解决您的问题,您应该执行以下操作。
只需替换代码行:
int likeCount = hit.getInt("siguncode");
关于这一点:
int likeCount = hit.getInt("sigungucode");
为firstimage编辑
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String creatorName = hit.getString("title");
String imageUrl = hit.getString("firstimage");
int likeCount = hit.getInt("sigungucode");
String firstimage = "";
if (hit.has("firstimage")) {
firstimage = hit.getString("firstimage");
}
mExampleList.add(new ExampleItem(imageUrl, creatorName, likeCount));
}
for(int i=0;i
您的代码逻辑基本正确。我已将您的代码复制到我的项目中并对其进行了测试。我发现了你的代码不起作用的原因
请查看下面的代码行
int likeCount = hit.getInt("siguncode");
json响应中没有字段siguncode
。相反,您有sigungucode
字段。这就是你的代码不起作用的原因
PS:JSONObject和JSONArray之间的区别
JSONObject只是一个具有键/值映射的对象
JSONArray是包含一个或多个JSONObject的集合
在JSON示例中,“header”
是JSONObject。
而“items”
中的“item”
是JSONArray
编辑
为了解决您的问题,您应该执行以下操作。
只需替换代码行:
int likeCount = hit.getInt("siguncode");
关于这一点:
int likeCount = hit.getInt("sigungucode");
为firstimage编辑
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String creatorName = hit.getString("title");
String imageUrl = hit.getString("firstimage");
int likeCount = hit.getInt("sigungucode");
String firstimage = "";
if (hit.has("firstimage")) {
firstimage = hit.getString("firstimage");
}
mExampleList.add(new ExampleItem(imageUrl, creatorName, likeCount));
}
for(int i=0;i
非常感谢您的回答。多亏了你,我才发现问题。所以我把siguncode改成siguncode。但现在还有另一个问题。org.json.JSONException:firstimage没有值。很明显,“firstimage”在我收到的数据上。我需要在“firstimage”中对url进行编码吗?非常感谢您再次给出答案。@Jay我能够获得firstimage
字段,而下面的代码行没有问题<代码>字符串firstimage=hit.getString(“firstimage”)代码>@Jay我刚发现一件事。有些对象没有获得firstimage
字段。这就是为什么它不起作用。我真的很感激你!!多亏了你,我才能解决这个问题@谢谢!!非常感谢你的回答。多亏了你,我才发现问题。所以我把siguncode改成siguncode。但现在还有另一个问题。org.json.JSONException:firstimage没有值。很明显,“firstimage”在我收到的数据上。我需要在“firstimage”中对url进行编码吗?非常感谢您再次给出答案。@Jay我能够获得firstimage
字段,而下面的代码行没有问题<代码>字符串firstimage=hit.getString(“firstimage”)代码>@Jay我刚发现一件事。有些对象没有获得firstimage
字段。这就是为什么它不起作用。我真的很感激你!!多亏了你,我才能解决这个问题@谢谢!!