gpt4 book ai didi

ios - 在 IOS swift 中从此 json 中提取字符串值

转载 作者:行者123 更新时间:2023-11-30 14:10:46 25 4
gpt4 key购买 nike

我正在使用 swiftyjson 来提取 json,但我无法提取规范 json,即规范内的标签和值。我很快就需要它。

((
{
code = "NPR 1515";
description = "With its enhanced power and performance the NPR1515 provides a genuine \"workhorse\" that will take almost anything in its stride.
\nThe full 1500 Watt motor unit packs almost 50% more power than normally required and this power is transmitted to the floor through our long established 150rpm, oil filled, low load, planetary gearbox .
\nThe big advantage of excess power is to provide scope for many additional tasks where the excess power is both advantageous and needed.";
id = 16;
name = "Scrubbers & Polisher - NPR 1515";
specification = "[{\"label\":\"Model No\",\"value\":\"PR 1515\\t\\t\\r\"},{\"label\":\"\\nMotor\",\"value\":\"500W\\t\\r\"},{\"label\":\"\\nPad\",\"value\":\"00mm\\t\\r\"},{\"label\":\"\\nPower\",\"value\":\"30V AC 50Hz\\r\"},{\"label\":\"\\nBrush\",\"value\":\"50mm\\t\\r\"},{\"label\":\"\\nSpeed\",\"value\":\"50 rpm\\r\"},{\"label\":\"\\nVacuum\",\"value\":\"T130\\t\\r\"},{\"label\":\"\\nRange\",\"value\":\"2m\\t\\t\\r\"},{\"label\":\"\\nWeight\",\"value\":\"0 Kgs\\t\\r\"},{\"label\":\"\\nSize\",\"value\":\"185 x 580x 450mm\"}]";
"video_url" = "<null>";
}
))

let json2 = JSON(data3!)
for (index, object) in json2 {
let name = object["name"].stringValue
let code = object["code"].stringValue
let description = object["description"].stringValue
let specification = object["specification"].stringValue

不运行这部分。

let json3 = JSON(specification)


for (index, object3) in json3 {
println("in this loop")
if let specification2 = object3["label"].string {
println(specification2)
}
else {
println(object3["label"].error)
}

let specification3 = object3["value"].stringValue

println(specification3)
}

现在它正在工作,但标签无法在标签中打印,它们正在 println 中工作,但如果我这样做,则该值有效,但标签无效。

  let str = self.labelArray[i]
let label8 = UILabel(frame: CGRectMake(2, 0, 0, 0))
label8.backgroundColor = UIColor.whiteColor()
label8.textColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
label8.frame = CGRect(x: 10, y: setheight , width: screenWidth/2, height: 25)
label8.textAlignment = NSTextAlignment.Left
label8.text = str
self.scrollview_add.addSubview(label8)

var label7 = UILabel(frame: CGRectMake(2, 0, 0, 0))
label7.backgroundColor = UIColor.whiteColor()
label7.textColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
label7.frame = CGRect(x: screenWidth/2, y: setheight , width: screenWidth/2, height: 25)
label7.textAlignment = NSTextAlignment.Right
label7.text = self.valueArray[i]
self.scrollview_add.addSubview(label7)

setheight += 25

最佳答案

正如您在问题中所发布的,规范值是一个字符串,因此首先您必须将其转换为NSData,然后将其转换为json:

 if let jsonData = specification.dataUsingEncoding(NSUTF8StringEncoding){
let json = JSON(data:jsonData)

for (index, object3) in json {
println("in this loop")
if let specification2 = object3["label"].string {
println(specification2)
}
else {
println(object3["label"].error)
}

let specification3 = object3["value"].stringValue

println(specification3)
}
}

关于ios - 在 IOS swift 中从此 json 中提取字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31801390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com