LoginSignup
yuki_H_Web
@yuki_H_Web (H yuki)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

reactでjsonのkey(?)に半角スペースがある場合の取り扱い方を知りたいです。

解決したいこと

key(?)に半角スペースがある場合の取り扱い方を知りたいです

{
    "horse number": {
        "0": 6,
        "1": 10,
        "2": 7,
        "3": 15,
        "4": 9,
        "5": 4,
        "6": 12,
        "7": 11,
        "8": 8,
        "9": 17,
        "10": 18,
        "11": 16,
        "12": 3,
        "13": 13,
        "14": 5,
        "15": 2,
        "16": 14
    }
}
import Data from '../../prediction.json';


const name = Data.horsename;ここの取り扱い方を知りたいです

自分で試したこと

jsonの”horse name"を無理やり"horsename"に置き換えています

0

1Answer

const name = Data["horse name"];

です。(元の JSON は "horse number" なのでこれでは取り出せませんが。)

key(?)

JSON の仕様では name ですが JavaScript のオブジェクトとしては (property) key です。どちらでも通じます。

2

Comments

  1. @yuki_H_Web

    Questioner
    ありがとうございます!
    とても助かりました!
    "horse number"、違う列のやつ持ってきてしまってました。。。

Your answer might help someone💌