在 Android Kotlin 中解析嵌套的 JSON

Posted

技术标签:

【中文标题】在 Android Kotlin 中解析嵌套的 JSON【英文标题】:Parsing Nested JSON in Android Kotlin 【发布时间】:2021-10-04 03:01:57 【问题描述】:

  data: 
    report: 
      dns_records: 
        ns: 
          records: [
            
              target: "ns2.google.com",
              ip: "216.239.34.10",
              country_code: "US",
              country_name: "United States of America",
              isp: "Google LLC"
            ,
            
              target: "ns1.google.com",
              ip: "216.239.32.10",
              country_code: "US",
              country_name: "United States of America",
              isp: "Google LLC"
            ,
            
              target: "ns4.google.com",
              ip: "216.239.38.10",
              country_code: "US",
              country_name: "United States of America",
              isp: "Google LLC"
            ,
            
              target: "ns3.google.com",
              ip: "216.239.36.10",
              country_code: "US",
              country_name: "United States of America",
              isp: "Google LLC"
            
          ]
        ,
        mx: 
          records: [
            
              target: "alt1.aspmx.l.google.com",
              ip: "209.85.202.26",
              country_code: "US",
              country_name: "United States of America",
              isp: "Google LLC"
            ,
            
              target: "alt2.aspmx.l.google.com",
              ip: "64.233.184.26",
              country_code: "US",
              country_name: "United States of America",
              isp: "Google LLC"
            ,
            
              target: "aspmx.l.google.com",
              ip: "173.194.216.26",
              country_code: "US",
              country_name: "United States of America",
              isp: "Google LLC"
            ,
            
              target: "alt4.aspmx.l.google.com",
              ip: "142.251.9.26",
              country_code: "US",
              country_name: "United States of America",
              isp: "Google LLC"
            ,
            
              target: "alt3.aspmx.l.google.com",
              ip: "142.250.153.26",
              country_code: "NL",
              country_name: "Netherlands",
              isp: "Google LLC"
            
          ]
        
      ,
      domain_blacklist: 
        engines: [
          
            name: "ThreatLog",
            reference: "https://www.threatlog.com/",
            detected: false
          ,
          
            name: "OpenPhish",
            reference: "https://openphish.com/",
            detected: false
          ,
          
            name: "PhishTank",
            reference: "https://www.phishtank.com/",
            detected: false
          ,
          
            name: "Phishing.Database",
            reference: "https://github.com/mitchellkrogza/Phishing.Database",
            detected: false
          ,
          
            name: "PhishStats",
            reference: "https://phishstats.info/",
            detected: false
          ,
          
            name: "URLVir",
            reference: "https://www.urlvir.com/",
            detected: false
          ,
          
            name: "URLhaus",
            reference: "https://urlhaus.abuse.ch/",
            detected: false
          ,
          
            name: "RPiList Not Serious",
            reference: "https://github.com/RPiList/specials",
            detected: false
          ,
          
            name: "AntiSocial Blacklist",
            reference: "https://theantisocialengineer.com/",
            detected: false
          ,
          
            name: "PhishFeed",
            reference: "https://phishfeed.com/",
            detected: false
          ,
          
            name: "NABP Not Recommended Sites",
            reference: "https://safe.pharmacy/buy-safely/",
            detected: false
          ,
          
            name: "Spam404",
            reference: "https://www.spam404.com/",
            detected: false
          ,
          
            name: "CRDF",
            reference: "https://threatcenter.crdf.fr/check.html",
            detected: false
          ,
          
            name: "Artists Against 419",
            reference: "http://wiki.aa419.org/index.php/Main_Page",
            detected: false
          ,
          
            name: "CERT Polska",
            reference: "https://www.cert.pl/",
            detected: false
          ,
          
            name: "Scam.Directory",
            reference: "https://scam.directory/",
            detected: false
          ,
          
            name: "PetScams",
            reference: "https://petscams.com/",
            detected: false
          ,
          
            name: "Suspicious Hosting IP",
            reference: "https://www.novirusthanks.org/",
            detected: false
          
        ],
        detections: 0
      ,
      file_type: 
        signature: "",
        extension: "",
        headers: "HTML"
      ,
      geo_location: 
        countries: [
          "US",
          "NL"
        ]
      ,
      html_forms: 
        number_of_total_forms: 1,
        number_of_total_input_fields: 8,
        two_text_inputs_in_a_form: false,
        credit_card_field_present: false,
        password_field_present: false,
        email_field_present: false
      ,
      redirection: 
        found: false,
        external: false,
        url: null
      ,
      response_headers: 
        code: 200,
        status: "HTTP/2 200",
        date: "Tue, 27 Jul 2021 20:32:40 GMT",
        expires: "-1",
        cache-control: "private, max-age=0",
        content-type: "text/html; charset=UTF-8",
        strict-transport-security: "max-age=31536000",
        p3p: "CP="ThisisnotaP3Ppolicy!Seeg.co/p3phelpformoreinfo."",
        server: "gws",
        x-xss-protection: "0",
        x-frame-options: "SAMEORIGIN",
        set-cookie: "1P_JAR=2021-07-27-20; expires=Thu, 26-Aug-2021 20:32:40 GMT; path=/; domain=.google.com; Secure; SameSite=none NID=220=nWPEsD7iVK5Co2H5yUqIi4wCPAesnjuPJ4lsxOdtSH5utroyMfIpGpk2hcGOqmJ_ZVCTh9LnxT7_qFpPDrCw2WvYS6sHl8KNuCXm3an8LFzqVReWDxgGzUXdQZ6meK3AR6xpOfbbal8OUpACK8dQdEJWl3Jm5KjWLtlirdiNwIE; expires=Wed, 26-Jan-2022 20:32:40 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=none",
        accept-ranges: "none",
        vary: "Accept-Encoding"
      ,
      risk_score: 
        result: 0
      ,
      security_checks: 
        is_host_an_ipv4: false,
        is_suspicious_url_pattern: false,
        is_suspicious_file_extension: false,
        is_robots_noindex: false,
        is_suspended_page: false,
        is_most_abused_tld: false,
        is_uncommon_clickable_url: false,
        is_phishing_heuristic: false,
        is_possible_emotet: false,
        is_suspicious_content: false,
        is_empty_page_title: false,
        is_empty_page_content: false,
        is_domain_blacklisted: false,
        is_suspicious_domain: false,
        is_sinkholed_domain: false,
        is_defaced_heuristic: false,
        is_masked_file: false,
        is_risky_geo_location: false,
        is_china_country: false,
        is_nigeria_country: false,
        is_non_standard_port: false,
        is_email_address_on_url_query: false,
        is_directory_listing: false,
        is_exe_on_directory_listing: false,
        is_zip_on_directory_listing: false,
        is_php_on_directory_listing: false,
        is_doc_on_directory_listing: false,
        is_pdf_on_directory_listing: false,
        is_linux_elf_file: false,
        is_linux_elf_file_on_free_dynamic_dns: false,
        is_linux_elf_file_on_free_hosting: false,
        is_linux_elf_file_on_ipv4: false,
        is_masked_linux_elf_file: false,
        is_masked_windows_exe_file: false,
        is_ms_office_file: false,
        is_windows_exe_file_on_free_dynamic_dns: false,
        is_windows_exe_file_on_free_hosting: false,
        is_windows_exe_file_on_ipv4: false,
        is_windows_exe_file: false,
        is_external_redirect: false,
        is_credit_card_field: false,
        is_password_field: false,
        is_valid_https: true
      ,
      server_details: 
        ip: "172.217.193.103",
        hostname: "",
        continent_code: "NA",
        continent_name: "North America",
        country_code: "US",
        country_name: "United States of America",
        region_name: "California",
        city_name: "Mountain View",
        latitude: 37.40599060058594,
        longitude: -122.0785140991211,
        isp: "Google LLC"
      ,
      site_category: 
        is_torrent: false,
        is_***_provider: false,
        is_free_hosting: false,
        is_anonymizer: false,
        is_url_shortener: false,
        is_free_dynamic_dns: false
      ,
      url_parts: 
        scheme: "https",
        host: "www.google.com",
        host_nowww: "google.com",
        port: null,
        path: "/",
        query: null
      ,
      web_page: 
        title: "Google",
        description: "",
        keywords: ""
      
    
  ,
  credits_remained: 1353.5,
  estimated_queries: "2,707",
  elapsed_time: "0.22",
  success: true

大家好,我是使用 Kotlin android 和解析 JSON 的新手 基本上我正在使用 OKhttp 将 JSONString 返回到我的应用程序,但我正在努力从返回的 JSON 中提取风险评分和安全检查对象,任何帮助将不胜感激.我相信 JSON 响应主要是在对象中构建的,但我不确定自从我使用 JSON 以来已经有一段时间了

【问题讨论】:

这甚至不是一个有效的json,你能发布真实的吗? 我不知道如何以不同的方式发布它我将 JSON 放入代码标签中,如果有帮助,我有端点的 URL,谢谢 只需复制您从服务中获得的内容并将其粘贴到此处,但根据jsonlint,这不是有效的 json 这对我不起作用对不起,谢谢您的回复 @Skizo-ozᴉʞS 我格式化了 json 并更新了问题。经版主审核后会获得批准。 【参考方案1】:
var basejsonObject = JSONObject(body)

            var datajsonObject = basejsonObject.getJSONObject("data")

            var reportjsonObject = datajsonObject.getJSONObject("report")

            var riskscorejsonObject = reportjsonObject.getJSONObject("risk_score")

我设法使用上述分解为对象的解决方案访问该对象

【讨论】:

您可以迁移到高级库,例如 Feign 或 Retrofit。尽管有这些强大的功能,OkHttp 没有内置的 JSON、XML 和其他内容类型的编码器/解码器。但是,我们可以借助 XML/JSON 绑定库来实现这些,或者我们可以使用 Feign 或 Retrofit 等高级库。查看这篇文章以获得更多见解。 baeldung.com/okhttp-json-response 我会检查一下谢谢你的输入@MohitAjwani

以上是关于在 Android Kotlin 中解析嵌套的 JSON的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Retrofit 2.0 (Kotlin) 正确解析嵌套的 JSON 对象?

如何通过改造将数组嵌套对象 Json 解析为 Kotlin?

Kotlin中实现RecyclerView嵌套RecyclerView

如何使用 Kotlin 在 Android 中处理从 Firebase Remote Config 解析 JSON 的效果方式?

无法解析来自库模块的 Kotlin Android 扩展布局

使用 kotlin-android-extensions 从子模块导入视图时未解析的引用