简体   繁体   中英

Extracting string values from this json in IOS swift

I am using swiftyjson to extract the json but i cant able to extract specification json that is label and value inside specification. I need it quickly.

((
    {
    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

does not run this part.

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)
                }

ok now it is working but label are not able to print in label they are working in println but if i do this value works but not label.

  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

According to the data structure you posted, the value inside "specification" is a string, not a JSON object. There're two ways to do it properly:

1, format the JSON data correctly, ie, instead of a string, the "specification" should also be a JSON object (array with dictionaries), depending on where you get this JSON data, you need to modify the generation logic of this data to accomplish this.

2, you can manually parse the specification using JSON parser, for example:

let error: NSErrorPointer = nil;
let specificationString = "[{\"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\"}]"

if let specificationData = specificationString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) {
    let specificationObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(specificationData, options: .MutableContainers, error: error)

    //Now you have a correct Array, do whatever you want with it.

}

As posted in your question value of specification is a string so first you have to convert it into NSData then convert it into 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)
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM