If you don't understand JSON analysis, please refer to the article here.
** [Swift5] Get array (individual) information from JSON using library'SwiftyJSON'** https://qiita.com/nkekisasa222/items/84c40bbfacfaca9f38a9
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
      }
    ]
  }
}]
Suppose that JSON format data exists like this, and you want to get the index number of an element that satisfies a certain condition. The conditions specified this time are that tone_name is" Joy "and" score "is 0.5 or more.
JsonAnalysisModel.swift
class JsonAnalysisModel {
    
    //Properties used in JSON parsing
    var count      = 0
    var arrayCount = 35
    var sampleJsonArray: [JSON] = []
    var joyCountArray  : [Any]  = []
    
    #abridgement
    
    //Method creation
    func startJsonAnalysis() {
        
        //Check JSON data (#Data displayed by checking JSON data)
        print(sampleJsonArray.debugDescription)
        
        //Iterate as many times as there are elements in sampleJsonArray (-Consistency at 1)
        for i in 0...arrayCount-1 {
            
            //JSON data"Joy" &&score is 0.Narrow down the conditions with 5 or more elements(Search for elements here)
            if sampleJsonArray[i]["document_tone"]["tones"][count]["score"] > 0.5 && sampleJsonArray[i]["document_tone"]["tones"][count]["tone_name"] == "Joy" {
                
                //Get the index number of the element that meets the conditions (-Consistency at 1)
                joyCountArray.append(sampleJsonArray[0].count+i-1)
            }
        }
        //Confirmation of acquisition result
        print("joyCountArray: \(joyCountArray)")
    }
}
Let's check the log.
joyCountArray: [0, 2, 3, 4, 5, 6, 11, 12, 13, 14, 17, 18, 19, 22, 23, 29, 30, 31, 32, 34]
Now you have ** analyzed JSON and got the index of the element that satisfies the condition **!
This time, I posted about ** How to analyze complex JSON and get the index of the element that satisfies the condition ** by applying JSON analysis. Since Swift performs JSON analysis frequently, it must be able to handle various conditions.
Thank you for watching until the end!
Recommended Posts