LoginSignup
1
0

More than 3 years have passed since last update.

permit()でJSONの複雑な形式を指定する手順

Posted at

元のJSON

 {
     "mark": 1,
     "offset": [23, 45],
     "Labels": [
         {
             "Name": "Rectangle",
             "Instances": [
                 {
                     "Box": {
                         "Width": 0.1,
                         "Height": 0.2
                     },
                     "Confidence": 99.9
                 }
             ]
         }
     ]
 }

配列の中がスカラー値のものは、値をすべて削除し、keyの前からvalue後のカンマ,の前までブレース{}で囲む

 {
     "mark": 1,
|    {"offset": []},
     "Labels": [
         {
             "Name": "Rectangle",
             "Instances": [
                 {
                     "Box": {
                         "Width": 0.1,
                         "Height": 0.2
                     },
                     "Confidence": 99.9
                 }
             ]
         }
     ]
 }

「連想配列」のvalueがスカラー値のものは、value部の記載を削除する(カンマ,は残す)

 {
|    "mark",
     {"offset": []},
     "Labels": [
         {
|            "Name",
             "Instances": [
                 {
                     "Box": {
|                        "Width",
|                        "Height"
                     },
|                    "Confidence"
                 }
             ]
         }
     ]
 }

keyの羅列しか残っていない「連想配列」は、元のブレース{}をブラケット[]に変え、keyの前からvalue後のカンマ,の前までブレース{}で囲む

 {
     "mark",
     {"offset": []},
     "Labels": [
         {
             "Name",
             "Instances": [
                 {
|                    {"Box": [
                         "Width",
                         "Height"
|                    ]},
                     "Confidence"
                 }
             ]
         }
     ]
 }

valueが「連想配列の配列」のものは、要素の2つ目以降を削除した後、ブレース{}をkeyの外側に持っていく

 {
     "mark",
     {"offset": []},
|    {"Labels": [
|
             "Name",
|            {"Instances": [
|
                     {"Box": [
                         "Width",
                         "Height"
                     ]},
                     "Confidence"
|
|            ]}
|
|    ]}
 }

最も外側の{}を削除し、permit()の引数にする

| params.permit(
     "mark",
     {"offset": []},
     {"Labels": [
             "Name",
             {"Instances": [
                     "Box": {
                         "Width",
                         "Height"
                     },
                     "Confidence"
             ]}
     ]}
| )

完成

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0