如何在iOS Swift中将对象数组转换为Json数组或Json字符串?

时间:2018-09-25 11:44:31

标签: ios json swift

我有一个数组,其中包含一组对象 root@68e4f88ab646:/# pkg-config --modversion opencv 3.4.2 “ QuizQu”是一个包含2个变量的类

var quiz_anwers_list = [QuizQu]()

现在有,

class QuizQu: Decodable {
    var ques_id: String?
    var their_answer: String?
}

这些打印的输出为:

for i in 0...self.quiz_anwers_list.count-1{

            print(self.quiz_anwers_list[i].ques_id ?? "no val in ques id of \(i)")
            print(self.quiz_anwers_list[i].their_answer ?? "no val in their_ans of \(i)")
        }

现在如何将其转换为JsonArray或JSON String?我是iOS的新手。

2 个答案:

答案 0 :(得分:0)

您的类可能应该是一个结构,并且应该符合if(sf::Keyboard::isKeyPressed(sf::Keyboard::B)) ,而不是Encodable,如果您打算进行编码和解码,则可以使用Decodable协议,该协议涵盖了两种情况。 / p>

完成此操作后,只需使用Codable将其转换为JSON数据,然后即可使用JSONEncoder打印它

String(bytes: Sequence, encoding: String.Encoding)

输出:

  

可选(“ [{{\” ques_id \“:\” 1 \“,\” their_answer \“:\” 2 \“},{\” ques_id \“:\” 2 \“,\” their_answer \ “:\” 2 \“},{\” ques_id \“:\” 3 \“,\” their_answer \“:\” 1 \“},{\” ques_id \“:\” 4 \“,\” them_answer \“:\” 4 \“},{\” ques_id \“:\” 5 \“,\” their_answer \“:\” 3 \“}]”“)

注意:输出的String被转义,因此反斜杠

答案 1 :(得分:0)

您可以使用

@page{
  size: A4;
  margin: 0;
  margin-bottom: 2cm;
}


@media print{

  body{
    padding: 2cm;
  }

  div#invoice-summary-table{
    page-break-inside: avoid;
  }

  div.footer { //this is the div I want to display
    display: block;
    text-align: center;
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;
    right: 0;
  }

  @bottom-center {
    font-size: 2em;
    font-family: sans-serif;
    color: #444;
    content: counter(page); //this is not displaying either
 }

 // I have tried pointing to the first page then to the div, after trying to use it inside @media page{ } and a few other options 

 @page :first div.footer {
   display: none;
 } 
}