亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java 和 json 對象

Java 和 json 對象

楊魅力 2023-08-04 14:59:52
我正在嘗試從鏈接下載包含最新新聞的 JSON 文件,然后使用 JSON 文件中的新聞文章填充新聞頁面,但我無法讓它工作。這是我的 JSON 文件:["sections": {  {    "title": "category 1",    "color": 2,    "posts": [      {        "title": "Test 1",        "date": 17-09-2019,        "images": {          "launcher_preview": "testimage.png",          "imageName2": "testimage.png"        },        "href": "https://testlink.com"      },      {        "title": "Test 2",        "date": 17-09-2019,        "images": {          "launcher_preview": "testimage2.png",          "imageName2": "testiamge2.png"        },        "href": "https://testlink2.com"      }    ]  },  {    "title": "category 2",    "color": 2,    "posts": [      {        "title": "Test 3",        "date": 17-09-2019,        "images": {          "launcher_preview": "testimage3.png",          "imageName2": "testimage3.png"        },        "href": "https://testlink3.com"      }    ]  }  }]我不確定是什么導致了這個錯誤,我認為我的 JSON 格式不正確,但我不確定,這里有人能看到是什么導致了這個錯誤嗎?
查看完整描述

3 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

如果您的對象位于數組中,則無法為其分配鍵。結果,你的HttpRequest.asJson()失敗了。我已經編輯了您的 JSON,將您的部分作為對象數組返回,而不是包含這些部分的單個數組對象。

此外,JSON 文件中不能將日期作為數字。我也將它們轉換為字符串。出于標準化目的,請確保將日期存儲為實際文件中的ISO 8601字符串。

嘗試一下 JSON 的編輯版本:

[

? {

? ? ? "title": "category 1",

? ? ? "color": 2,

? ? ? "posts": [{

? ? ? ? ? ? ? "title": "Test 1",

? ? ? ? ? ? ? "date": "17-09-2019",

? ? ? ? ? ? ? "images": {

? ? ? ? ? ? ? ? ? "launcher_preview": "testimage.png",

? ? ? ? ? ? ? ? ? "imageName2": "testimage.png"

? ? ? ? ? ? ? },

? ? ? ? ? ? ? "href": "https://testlink.com"

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? ? "title": "Test 2",

? ? ? ? ? ? ? "date": "17-09-2019",

? ? ? ? ? ? ? "images": {

? ? ? ? ? ? ? ? ? "launcher_preview": "testimage2.png",

? ? ? ? ? ? ? ? ? "imageName2": "testiamge2.png"

? ? ? ? ? ? ? },

? ? ? ? ? ? ? "href": "https://testlink2.com"

? ? ? ? ? }

? ? ? ]

? },

? {

? ? ? "title": "category 2",

? ? ? "color": 2,

? ? ? "posts": [{

? ? ? ? ? "title": "Test 3",

? ? ? ? ? "date": "17-09-2019",

? ? ? ? ? "images": {

? ? ? ? ? ? ? "launcher_preview": "testimage3.png",

? ? ? ? ? ? ? "imageName2": "testimage3.png"

? ? ? ? ? },

? ? ? ? ? "href": "https://testlink3.com"

? ? ? }]

? }

]


查看完整回答
反對 回復 2023-08-04
?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

我看到三個問題

1. json 對象周圍的括號錯誤。

2.Sections 是一個數組,但缺少數組語法。

3. 日期字符串不是有效的對象類型,該字符串應該用引號引起來。具有部分的對象的正確格式的 json,它是兩個部分的數組。


{"sections": [

  {

    "title": "category 1",

    "color": 2,

    "posts": [

      {

        "title": "Test 1",

        "date": "17-09-2019",

        "images": {

          "launcher_preview": "testimage.png",

          "imageName2": "testimage.png"

        },

        "href": "https://testlink.com"

      },

      {

        "title": "Test 2",

        "date": "17-09-2019",

        "images": {

          "launcher_preview": "testimage2.png",

          "imageName2": "testiamge2.png"

        },

        "href": "https://testlink2.com"

      }

    ]

  },

  {

    "title": "category 2",

    "color": 2,

    "posts": [

      {

        "title": "Test 3",

        "date": "17-09-2019",

        "images": {

          "launcher_preview": "testimage3.png",

          "imageName2": "testimage3.png"

        },

        "href": "https://testlink3.com"

      }

    ]

  }]

}


查看完整回答
反對 回復 2023-08-04
?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

[
    "sections": {
  {

我在文件的開頭看到兩個問題。

第一,第一個字符是方括號,表示包含的值將是一個簡單列表。但隨后它直接進入"sections" : {,這是一個鍵/值語法,表明我們應該處于字典/哈希圖上下文中。但我們不是;我們處于列表上下文中。

其次,后面有兩個左大括號"sections":。第二個是想說明什么?


查看完整回答
反對 回復 2023-08-04
  • 3 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號