相关疑难解决方法(0)

如何从JSONObject的路径中获取嵌套值?

我正在尝试实现给定any JSONObject和path 的函数,该函数String将返回与该路径对应的对象属性。

例如,给定以下json:

{
"name": "John", 
"friends": [
  {"name": "Paul",
   "age":42},
  {"name": "Peter",
   "age":24}
 ],
"address": {"city": "London"}
}
Run Code Online (Sandbox Code Playgroud)
  • getAttribute(jsonObject, "name") 应该回来 "John"
  • getAttribute(jsonObject, "address.city") 应该回来 "London"
  • getAttribute(jsonObject, "friends[0].name") 应该回来 "Paul"

请注意,此JSON只是一个示例,jsonObject没有预定义的结构,可以表示任何有效的json。

我编写了实现前两种情况的第一个版本,但是处理数组和多级数组"foo[0][0].bar"给此函数带来了很多复杂性。

是否有推荐的工具/库/方法,用于从给定“复杂”路径的JSONObject获取属性?

java parsing android json

5
推荐指数
1
解决办法
1万
查看次数

如何使用GSON在android中解析动态"key"的JSON

我一直在使用GSON库来解析所有json字符串并获取一个JSON对象.但现在我需要解析是这样的:

{
   "status":1,
   "info":[
      {
         "\u5a31\u4e50":"\u51b7\u76d8,\u9ad8\u811a\u676f,\u6211\u7684\u7cd6\u679c\u5c4b,\u670d\u52a1\u4e1a\u6d88\u8d39\u52b5"
      },
      {
         "\u7f8e\u5986":"\u4e2a\u62a4|\u5316\u5986#\u9762\u90e8\u62a4\u7406,\u4e2a\u4eba\u536b\u751f,\u8eab\u4f53\u62a4\u7406,\u9999\u6c34\u9999\u6c1b,\u6c90\u6d74|\u7f8e\u53d1\u7528\u54c1,\u5f69\u5986,\u7cbe\u6cb9SPA,\u773c\u90e8\u62a4\u7406,\u78e8\u7802\u53bb"
      },
      {
         "\u8863\u670d":"\u670d|\u9970|\u978b|\u5e3d#\u670d\u88c5,\u978b\u9774,\u5185\u8863,\u914d\u9970,\u536b\u8863,\u4f11\u95f2\u88e4,T\u6064,\u88d9\u5b50,\u886c\u886b,\u9488\u7ec7\u886b,\u5a74\u5e7c\u513f\u670d\u9970"
      }
   ],
   "total":3
}
Run Code Online (Sandbox Code Playgroud)

关键字段是动态的,所以我不知道如何编写模型类来读取它.

android gson

4
推荐指数
1
解决办法
6809
查看次数

在 Android 中使用 Gson 从通知数据中解析嵌套的 JSON 数组

我正在从 FCM 通知中获取 JSON 格式的数据。它具有基于通知类型的不同格式,如下所示:

格式 1:accept_request:

{
   "alert": "Accept Request By driver",
   "title": "Accept Request By driver",
   "booking_id": "247",
   "notification_type": "accept_request"
}
Run Code Online (Sandbox Code Playgroud)

格式 2:end_request:

{
    "alert": "End trip By driver",
    "title": "End trip By driver",
    "booking_id": "247",
    "notification_type": "end_request",
    "servicesList": [
        {
            "service_id": "1",
            "service_name": "Services1",
            "status": "true",
            "sub_category": [
                {
                    "sub_cat_id": "1",
                    "sub_cat_name": "Doctors on call",
                    "service_cost": "15.00",
                    "service_cat_id": "1",
                    "cost": 0
                }
            ],
            "is_multiple_choose": "0",
            "total_cost": 15
        }
}
Run Code Online (Sandbox Code Playgroud)

我已经创建CommonNotificationBean.java为 POJO 类。我已经成功解析了accept_request.

它的工作是因为所有键都有字符串值并且 …

android json gson firebase-notifications

0
推荐指数
1
解决办法
1823
查看次数

标签 统计

android ×3

gson ×2

json ×2

firebase-notifications ×1

java ×1

parsing ×1