Si vous ne comprenez pas l'analyse JSON, veuillez vous référer à l'article ici.
** [Swift5] Utilisez la bibliothèque'SwiftyJSON pour obtenir des informations de tableau (individuelles) à partir de JSON ** 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
}
]
}
}]
Supposons que vous ayez des données au format JSON comme celles-ci et que vous souhaitiez obtenir le numéro d'index d'un élément qui remplit certaines conditions. Les conditions spécifiées cette fois sont que tone_name est" Joy "et" score "est de 0,5 ou plus
.
JsonAnalysisModel.swift
class JsonAnalysisModel {
//Propriétés utilisées dans l'analyse JSON
var count = 0
var arrayCount = 35
var sampleJsonArray: [JSON] = []
var joyCountArray : [Any] = []
#réduction
//Création de méthode
func startJsonAnalysis() {
//Confirmation des données JSON (#Données affichées en vérifiant les données JSON)
print(sampleJsonArray.debugDescription)
//Itérez autant de fois qu'il y a d'éléments dans sampleJsonArray (-Cohérence à 1)
for i in 0...arrayCount-1 {
//Données JSON"Joy" &&le score est de 0.Affinez les conditions avec 5 éléments ou plus(Rechercher des éléments ici)
if sampleJsonArray[i]["document_tone"]["tones"][count]["score"] > 0.5 && sampleJsonArray[i]["document_tone"]["tones"][count]["tone_name"] == "Joy" {
//Obtenez le numéro d'index de l'élément qui remplit les conditions (-Cohérence à 1)
joyCountArray.append(sampleJsonArray[0].count+i-1)
}
}
//Confirmation du résultat d'acquisition
print("joyCountArray: \(joyCountArray)")
}
}
Vérifions le journal.
joyCountArray: [0, 2, 3, 4, 5, 6, 11, 12, 13, 14, 17, 18, 19, 22, 23, 29, 30, 31, 32, 34]
Vous avez maintenant ** analysé le JSON et obtenu l'index de l'élément qui remplit les conditions **!
Cette fois, j'ai posté sur ** Comment analyser un JSON complexe et obtenir l'index de l'élément qui satisfait à la condition ** en appliquant une analyse JSON. Étant donné que Swift effectue fréquemment des analyses JSON, il doit être capable de gérer diverses conditions.
Merci d'avoir regardé jusqu'à la fin!