如何在没有JsonArray名称的情况下获取Json对象

编程入门 行业动态 更新时间:2024-10-27 05:24:01
本文介绍了如何在没有JsonArray名称的情况下获取Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试解析从以下链接检索到的JSON数据

I am trying to parse the JSON data retrieved from the following link

fipeapi.appspot/api/1/carros /marcas.json

它没有JsonArray的名称.到目前为止,这是我尝试过的.

It does not have a name of the JsonArray. Here is what I have tried so far.

private String getName(int position) { String name = ""; try { //Getting object of given index JSONObject json = result.getJSONObject(position); //Fetching name from that object name = json.getString(Config.TAG_NAME); } catch (JSONException e) { e.printStackTrace(); } //Returning the name return name; }

这是Config类

public class Config { //JSON URL public static final String DATA_URL = "fipeapi.appspot/api/1/carros/marcas.json"; //Tags used in the JSON String public static final String TAG_USERNAME = "name"; public static final String TAG_NAME = "fipe_name"; public static final String TAG_COURSE = "key"; public static final String TAG_ID_MARCA_CARRO = "id"; //JSON array name public static final String JSON_ARRAY = "marcas"; }

如果您需要更多信息来帮助我解决此问题,请告诉我.预先感谢!

Please let me know if you need more information to help me in solving this problem. Thanks in advance!

推荐答案

在Android中解析JSON数据的更简单方法是使用 Gson .与您的代码集成起来既简单又容易.您只需要在build.gradle文件中添加以下依赖项.

The easier way to parse a JSON data in Android is using Gson. It is simple and easier to integrate with your code. You just need to add the following dependency in your build.gradle file.

dependencies { implementation 'com.google.code.gson:gson:2.8.5' }

现在,在代码中,您需要创建以下类.

Now in your code, you need to create the following class.

public class Car { public String name; public String fipe_name; public Integer order; public String key; public Long id; }

现在只需解析您具有的JSON字符串,如下所示.

Now simply parse the JSON string you have like the following.

Gson gson = new Gson(); Car[] carList = gson.fromJson(jsonResponse, Cars[].class);

您会发现将JSON解析为对象数组.希望能有所帮助.

You will find the JSON parsed as an array of objects. Hope that helps.

更多推荐

如何在没有JsonArray名称的情况下获取Json对象

本文发布于:2023-11-06 14:46:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:情况下   对象   名称   如何在   JsonArray

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!