gpt4 book ai didi

android - 如何解析四方类别json

转载 作者:行者123 更新时间:2023-11-30 03:44:48 25 4
gpt4 key购买 nike

基本上我想做的是使用类别从 foursquare 中过滤 field ,但我想让用户选择他们想要的“过滤器”类型,但到目前为止我无法正确解析 json。

原始json数据eg

{
"meta": {
"code": 200
},
"response": {
"categories": [
{
"id": "4d4b7104d754a06370d81259",
"name": "Arts & Entertainment",
"pluralName": "Arts & Entertainment",
"shortName": "Arts & Entertainment",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/default_",
"suffix": ".png"
},
"categories": [
{
"id": "4fceea171983d5d06c3e9823",
"name": "Aquarium",
"pluralName": "Aquariums",
"shortName": "Aquarium",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/aquarium_",
"suffix": ".png"
},
"categories": [

]
},
{
"id": "4bf58dd8d48988d1e1931735",
"name": "Arcade",
"pluralName": "Arcades",
"shortName": "Arcade",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/arcade_",
"suffix": ".png"
},
"categories": [

]
},
{
"id": "4bf58dd8d48988d1e2931735",
"name": "Art Gallery",
"pluralName": "Art Galleries",
"shortName": "Art Gallery",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/artgallery_",
"suffix": ".png"
},
"categories": [

]
},
{
"id": "4bf58dd8d48988d1e4931735",
"name": "Bowling Alley",
"pluralName": "Bowling Alleys",
"shortName": "Bowling Alley",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/bowling_",
"suffix": ".png"
},
"categories": [

]
},
{
"id": "4bf58dd8d48988d17c941735",
"name": "Casino",
"pluralName": "Casinos",
"shortName": "Casino",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/casino_",
"suffix": ".png"
},
"categories": [

]
},
{
"id": "4bf58dd8d48988d18e941735",
"name": "Comedy Club",
"pluralName": "Comedy Clubs",
"shortName": "Comedy Club",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/comedyclub_",
"suffix": ".png"
},
"categories": [

]

bla bla bla

并使用我现在拥有的代码如下:

             JSONObject jsonObj  = new JSONObject(response);
jsonObj = jsonObj.getJSONObject("response");
JSONArray groups;// = (JSONArray) jsonObj.getJSONObject("response").getJSONArray("Categories);
groups = jsonObj.getJSONArray("categories");

FileOutputStream fos = WhereToEatLehActivity.this.openFileOutput("file_name"+".txt",Context.MODE_PRIVATE);
Writer out = new OutputStreamWriter(fos);
out.write(jsonObj.toString());
out.close();
int length = jsonObj.length();


if (length > 0) {

for (int i = 0; i < length; i++) {
JSONObject aCat = (JSONObject) groups.get(i);
Log.d("category", aCat.toString());
if(aCat.getString("name").equalsIgnoreCase("Food")){
Log.d("food", aCat.getString("id"));
}


}

查看运行输出的txt文件,我的json如下:

  "categories": [
{
"id": "4d4b7104d754a06370d81259",
"icon": {
"suffix": ".png",
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/default_"
},
"categories": [
{
"id": "4fceea171983d5d06c3e9823",
"icon": {
"suffix": ".png",
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/aquarium_"
},
"categories": [

],
"shortName": "Aquarium",
"pluralName": "Aquariums",
"name": "Aquarium"
},
{
"id": "4bf58dd8d48988d1e1931735",
"icon": {
"suffix": ".png",
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/arcade_"
},
"categories": [

],
"shortName": "Arcade",
"pluralName": "Arcades",
"name": "Arcade"
},

从那里开始,json 数据已经是错误的,因为 json 数据被删减得太多以至于我无法使用该名称,但我不知道我做错了什么,任何指针家伙?

最佳答案

改变

 int length  = jsonObj.length();

 int length = groups.length();

在 for 循环条件中使用 JsonArray 而不是 JsonObject 中的项目数

关于android - 如何解析四方类别json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15163368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com