Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

[Swift5]複雑なJSONの解析をおこない、条件を満たす要素のindexを取得する方法

はじめに

JSON解析についてわからない方はコチラ▼の記事を参考にしてください。

[Swift5]ライブラリ 'SwiftyJSON' を使ってJSONから配列(個別)の情報を取得する
https://qiita.com/nkekisasa222/items/84c40bbfacfaca9f38a9

JSONデータの確認

joyCountArray.
[{
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.53749899999999995,
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  },
  "sentences_tone" : [
    {
      "tones" : [

      ],
      "text" : "Kozai \"Aza\" Haruka: Haruka, Ie no Haruka: Haruka, Ie no Haruka, 'The body is too clean.'",
      "sentence_id" : 0
    },
    {
      "tones" : [

      ],
      "text" : "(Entame NEXT)",
      "sentence_id" : 1
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.67608400000000002,
        "tone_name" : "Joy"
      },
      {
        "tone_id" : "analytical",
        "score" : 0.92818699999999998,
        "tone_name" : "Analytical"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.74235700000000004,
        "tone_id" : "joy"
      }
    ]
  },
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "tones" : [

      ],
      "text" : "Ariyoshi is also a lavatory?"
    },
    {
      "sentence_id" : 1,
      "tones" : [
        {
          "tone_name" : "Joy",
          "tone_id" : "joy",
          "score" : 0.75596300000000005
        }
      ],
      "text" : "Mihopa's \"overwhelmable beauty Body\" sexy water shoe shots are released (Entame NEXT)"
    }
  ]
}, {
  "sentences_tone" : [
    {
      "text" : "Yuzuno Mizusawa, a beautiful woman with a hairstyle of blue hair, is released from hair color to a single one-peoped kamiwanpi with a blue-leg.",
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_id" : "joy",
          "tone_name" : "Joy",
          "score" : 0.76069399999999998
        }
      ]
    },
    {
      "sentence_id" : 1,
      "text" : "(Entame NEXT)",
      "tones" : [

      ]
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.76504700000000003,
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "sentences_tone" : [
    {
      "text" : "Princess Inster of the Graviar world, Inster Saki Saki, said, \"I may not have felt any resistance to escaping from the beginning.\"",
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_id" : "joy",
          "score" : 0.64593199999999995,
          "tone_name" : "Joy"
        },
        {
          "tone_id" : "tentative",
          "score" : 0.86333300000000002,
          "tone_name" : "Tentative"
        }
      ]
    },
    {
      "text" : "(Entame NEXT)",
      "sentence_id" : 1,
      "tones" : [

      ]
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.65832100000000005,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      },
      {
        "score" : 0.98048800000000003,
        "tone_name" : "Tentative",
        "tone_id" : "tentative"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.70347899999999997,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "text" : "\"Shiratama\" in \"The Edge of the Bend\" and the dog dog \"Shiratama\" (The Dog Warrior), a dog in the Twin Till, the dog of the Twin Tin.",
      "tones" : [

      ]
    },
    {
      "sentence_id" : 1,
      "text" : "(Entame Next)",
      "tones" : [

      ]
    }
  ],
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Analytical",
        "score" : 0.61642600000000003,
        "tone_id" : "analytical"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.58929500000000001,
        "tone_id" : "analytical",
        "tone_name" : "Analytical"
      }
    ]
  }
}, {
  "sentences_tone" : [
    {
      "text" : "Perfume, grag, grag, chalen, grag, grag!",
      "sentence_id" : 0,
      "tones" : [

      ]
    },
    {
      "text" : "The two shots that were too beautiful were the subject of the buzzo (Entame Next).",
      "sentence_id" : 1,
      "tones" : [
        {
          "tone_name" : "Joy",
          "score" : 0.80030299999999999,
          "tone_id" : "joy"
        }
      ]
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.79371400000000003,
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.63750700000000005,
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.83795299999999995,
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_name" : "Joy",
          "tone_id" : "joy",
          "score" : 0.67072399999999999
        }
      ],
      "text" : "\"Bomb Girl,\" \"Bomb Girl,\" with a limited number of New Hare Acaler in the public, \"It's getting more and more cute.\""
    },
    {
      "sentence_id" : 1,
      "tones" : [

      ],
      "text" : "(Entame NEXT)"
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.69078499999999998,
        "tone_id" : "joy",
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_id" : "tentative",
          "tone_name" : "Tentative",
          "score" : 0.71630099999999997
        }
      ],
      "text" : "\"I'm happy just looking at the scene of the popular Cossprayer ShenInin uniform.\""
    },
    {
      "sentence_id" : 1,
      "tones" : [

      ],
      "text" : "(Entame NEXT)"
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.94484500000000005,
        "tone_name" : "Tentative",
        "tone_id" : "tentative"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.77552100000000002,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      }
    ]
  },
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "tones" : [
        {
          "tone_id" : "joy",
          "score" : 0.62795699999999999,
          "tone_name" : "Joy"
        }
      ],
      "text" : "Followers: 32.4 million!"
    },
    {
      "sentence_id" : 1,
      "tones" : [
        {
          "tone_id" : "joy",
          "tone_name" : "Joy",
          "score" : 0.75022100000000003
        }
      ],
      "text" : "The Queen of the Gravier and the Queen of the Gravies of the Gravies and the Queen of the Sainting and the Mine Tree"
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.83205300000000004,
        "tone_id" : "joy",
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "tone_id" : "joy",
        "score" : 0.53014799999999995
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "sentences_tone" : [
    {
      "text" : "\"The thighs are thicker.\"",
      "tones" : [

      ],
      "sentence_id" : 0
    },
    {
      "text" : "Yuho HONDA, Cute and Sexual Dresses (Entame NEXT)",
      "tones" : [
        {
          "tone_id" : "joy",
          "score" : 0.75571100000000002,
          "tone_name" : "Joy"
        }
      ],
      "sentence_id" : 1
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.73423000000000005,
        "tone_name" : "Joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "score" : 0.67583000000000004,
        "tone_name" : "Joy"
      }
    ]
  },
  "sentences_tone" : [
    {
      "text" : "\"Detective Conan\" with voice actors and sumptues, \"Detective Conan,\" which is too much of a love for \"Detective Conan,\" and \"Sumitai Edogawa.\"",
      "tones" : [
        {
          "tone_id" : "joy",
          "tone_name" : "Joy",
          "score" : 0.70681300000000002
        }
      ],
      "sentence_id" : 0
    },
    {
      "text" : "(\"Entame NEXT\")",
      "tones" : [

      ],
      "sentence_id" : 1
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Analytical",
        "tone_id" : "analytical",
        "score" : 0.84622399999999998
      },
      {
        "tone_name" : "Tentative",
        "tone_id" : "tentative",
        "score" : 0.61621599999999999
      }
    ]
  },
  "sentences_tone" : [
    {
      "text" : "\"I don't feel comfortable with the Dokidoki\" (Entame NEXT), which is also known as \"Suta Akari,\" and the girl's favorite helcey.",
      "tones" : [
        {
          "score" : 0.58152999999999999,
          "tone_name" : "Joy",
          "tone_id" : "joy"
        }
      ],
      "sentence_id" : 0
    },
    {
      "text" : "\" I don't feel like a dog",
      "tones" : [
        {
          "tone_name" : "Analytical",
          "score" : 0.94455100000000003,
          "tone_id" : "analytical"
        }
      ],
      "sentence_id" : 1
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Analytical",
        "score" : 0.82533800000000002,
        "tone_id" : "analytical"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "document_tone" : {
    "tones" : [

    ]
  }
}, {
  "sentences_tone" : [
    {
      "sentence_id" : 0,
      "text" : "Mo Musume.Makino Mari, a swimsuit, and a sneaker's legendable offshot: \"You really are a goddess.\"",
      "tones" : [
        {
          "tone_name" : "Analytical",
          "score" : 0.50676299999999996,
          "tone_id" : "analytical"
        }
      ]
    },
    {
      "sentence_id" : 1,
      "tones" : [

      ],
      "text" : "(Entame NEXT)"
    }
  ],
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Analytical",
        "tone_id" : "analytical",
        "score" : 0.64375800000000005
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.66994399999999998,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_name" : "Joy",
        "score" : 0.91817700000000002,
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "score" : 0.61852099999999999,
        "tone_name" : "Joy",
        "tone_id" : "joy"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "tone_name" : "Joy",
        "score" : 0.50842900000000002
      }
    ]
  },
  "sentences_tone" : [
    {
      "text" : "Queen Inster in the Graviar world, Inster-Sakuchimi, said: \"Anyway, I'm aware of this by shooting.\"",
      "tones" : [

      ],
      "sentence_id" : 0
    },
    {
      "text" : "(\"Entame Next\")",
      "tones" : [

      ],
      "sentence_id" : 1
    }
  ]
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "sadness",
        "score" : 0.503888,
        "tone_name" : "Sadness"
      },
      {
        "tone_id" : "analytical",
        "score" : 0.57936699999999997,
        "tone_name" : "Analytical"
      }
    ]
  }
}, {
  "document_tone" : {
    "tones" : [
      {
        "tone_id" : "joy",
        "tone_name" : "Joy",
        "score" : 0.84084999999999999
      }
    ]
  }
}]

このようにJSON形式のデータが存在しており、とある条件を満たす要素のインデックス番号を取得したいとします。今回指定する条件は、tone_nameが "Joy" かつ "score" が0.5以上とします。

条件をコードに落とし込む

JsonAnalysisModel.swift
class JsonAnalysisModel {

    //JSON解析で使用するプロパティ
    var count      = 0
    var arrayCount = 35
    var sampleJsonArray: [JSON] = []
    var joyCountArray  : [Any]  = []

    #省略

    //メソッド作成
    func startJsonAnalysis() {

        //JSONデータの確認(#JSONデータの確認で表示したデータ)
        print(sampleJsonArray.debugDescription)

        //sampleJsonArrayの要素数だけ繰り返し処理を行う(-1で整合性)
        for i in 0...arrayCount-1 {

            //JSONデータが "Joy" && scoreが0.5以上の要素で条件を絞る(ここで要素を検索)
            if sampleJsonArray[i]["document_tone"]["tones"][count]["score"] > 0.5 && sampleJsonArray[i]["document_tone"]["tones"][count]["tone_name"] == "Joy" {

                //条件を満たした要素のindex番号の取得(-1で整合性)
                joyCountArray.append(sampleJsonArray[0].count+i-1)
            }
        }
        //取得結果の確認
        print("joyCountArray: \(joyCountArray)")
    }
}

ではログを確認してみましょう。

joyCountArray: [0, 2, 3, 4, 5, 6, 11, 12, 13, 14, 17, 18, 19, 22, 23, 29, 30, 31, 32, 34]

これでJSONの解析をおこない、条件を満たす要素のindexの取得ができましたね!

最後に

今回はJSON解析の応用で複雑なJSONの解析をおこない、条件を満たす要素のindexを取得する方法について投稿しました。Swiftでは頻繁にJSON解析をおこなうので様々な条件に対応できるようにならないといけませんね。

最後までご覧いただきありがとうございました!

nkekisasa222
理念:エンジニアリングを通じて社会と人々の生活に潜む問題を解決し他者貢献すること/Swiftにて個人アプリ開発中/iOSエンジニアへの転職を目指しております/勤務希望地: 東京都/学習のアウトプットでQiitaを投稿しています/24歳/エンジニアと人生コミュニティ/宜しくお願いします!
https://github.com/ken-sasaki-222
engineerlife
技術力をベースに人生を謳歌する人たちのコミュニティです。
https://community.camp-fire.jp/projects/view/280040
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away