0

I want to parse the json string and displays in the UITextView. I am getting the data with HTML string with key and values. This is the below sample string response from the server,

   {"":"<strong>Adidas<\/strong><br>Enjoy 10% off","Period":"10 Oct 2019 - 11 Nov 2019","Description":"<p>Enjoy 30% off 
   "Terms & Conditons":"<ol>\n<li>It's available only in peak hours<\/li>\n}

I need to display like the below format with HTML,

Expected output: 
      Adidas
      Enjoy 10% off
      Period:
      10 Oct 2019 - 11 Nov 2019
      Description:
      Enjoy 30% off.
      Terms & Conditons:
      It's available only in peak hours

I've used dictionary and extracted the data but the problem is order of the data is not in sequence.

   func setupData(responseString : String)
   {
    // Convert string to dictionary for extracting the keys and values
    // Need to maintain the order while adding the data
    let content = convertToDictionary(text: responseString)
    var text : String = ""
    if let contentDataDict = content
    {
        for (key, value) in  contentDataDict
        {
         // want to append the key and values in the sequence order
                textView.attributedText = text.htmlToAttributedString
            }

        }
    } 

How can I parse and display the data as per the expected output.

Devan
  • 147
  • 1
  • 1
  • 8
  • Have a look at this question: https://stackoverflow.com/questions/51092027/swift-4-keeping-the-same-order-when-parsing-a-json – Kevin Oct 21 '19 at 06:59
  • Do you mean the data is out of order within the record, or the record themselves are out of order? If the former, then you'll either need to use a different data structure as dictionaries have no order, or use the keys to extract the data in the order you want. – flanker Oct 21 '19 at 07:14
  • @flanker, Data is in the correct order. When I am using dictionary order is not in the sequence. Are there any other mechanism to achieve this? thanks. – Devan Oct 21 '19 at 08:40
  • @devan, do you know in advance what the keys will be, and will the always be the same? Or is the problem that really they aren't keys and values but just a pair of data values? – flanker Oct 21 '19 at 08:48
  • @flanker, Mostly response key should be the same. May be some key values are presents or not depends on the requirement. But the order needs to be maintained as per the response. I think dictionary won't work this case right? – Devan Oct 21 '19 at 09:34
  • @Devan, yep, spot on. I've added an outline solution below that will allow you to preserve the order. You'll need to flesh it out with your current code for html processing & updating the UI – flanker Oct 21 '19 at 11:30

1 Answers1

0

Devan, you're right in your thinking that a dictionary won't work. You'd be better loading the html into an array. I've used pseudocode as to represent a new convertToArray to replace your convertToDictionary as you didn't supply all the original code

struct Component {
  field: String
  value: String
}

func convertToArray(response: String) -> [Component] [
  let components = [Component]()
  for entries in response {
    //extract key/value pairs from html as before
    //then create a component struct with them and add it to the array
    components.append(Component(field: key, value: value)
  }
  return components //now a sequence of structs represent your data in order
}


func setupData(responseString : String) {
  let components: [Component] = convertToArray(text: responseString)
  for component in components {
     //add component.field and component.value to your attributed text string
     //and then display as before
  }
}

flanker
  • 3,840
  • 1
  • 12
  • 20