【问题标题】:Parsing an array sent from database解析从数据库发送的数组
【发布时间】:2015-03-22 21:49:45
【问题描述】:

您好,我想知道如何尝试解析从数据库中检索到的数据:

[
   {
      "q_id":"1",
      "type":"for",
      "author":"kappa420",
      "question":"what is the meaning of life?",
      "answer":"good question."
   },
   {
      "q_id":"2",
      "type":"recursive",
      "author":"kappa420",
      "question":"If only you knew",
      "answer":"That I was right behind you!"
   },
   {
      "q_id":"3",
      "type":"while",
      "author":"kappa420",
      "question":"who are you?",
      "answer":"ha-HA!"
   },
   {
      "q_id":"5",
      "type":"testtyte",
      "author":"testauthor",
      "question":"testquestion",
      "answer":""
   },
   {
      "q_id":"6",
      "type":"testtyte",
      "author":"testauthor",
      "question":"testquestion",
      "answer":"testanswer"
   },
   {
      "q_id":"7",
      "type":"public int add( int num1,",
      "author":"",
      "question":"Given two numbers the user must be able to add these numbers. ",
      "answer":" add(5,4) = 9\r\n add(2,3) = 5"
   },
   {
      "q_id":"8",
      "type":"public int add( int num1,",
      "author":"",
      "question":"Given two numbers the user must be able to add these numbers. ",
      "answer":" add(5,4) = 9\r\n add(2,3) = 5"
   },
   {
      "q_id":"9",
      "type":"dasdsa",
      "author":"",
      "question":" adddfa",
      "answer":" asdfs"
   },
   {
      "q_id":"10",
      "type":"adfafd",
      "author":"",
      "question":" dsadsf",
      "answer":" saddasf"
   },
   {
      "q_id":"11",
      "type":"adsf",
      "author":"",
      "question":" adsfs",
      "answer":" dsadsf"
   }
]

有没有办法获取某些元素,如 q_id 和 author 并将它们放入 html 属性中?

【问题讨论】:

  • 您能否解释一下这些数据是如何提供给页面的(作为 JSON 或 JavaScript 对象,也可能通过 AJAX)?
  • II 到来自服务器的 curl 请求并且服务器响应,然后我使用 PHP 和 jQuery 将其打印出来。

标签: php html arrays


【解决方案1】:

您可以使用命令 JSON.parse('{JSON HERE}') 将您的 JSON 解析为 JavaScript 数组,您可以对您的数组执行 for 循环并为它们创建一个 HTML 元素,如下所示:

$.each(arrayVar, function(i, obj) {
    $element = $('<div></div>');
    $element.attr('id') = obj['q_id'];
    $('body').append($element);
});

我希望这就是你要找的东西!

【讨论】:

    【解决方案2】:

    这是一个json字符串,用php很容易解析成数组。

    $DataArray =  json_decode($MyDatabaseString,True);
    

    【讨论】:

      【解决方案3】:
                       $results = //db result
                        foreach($results as $result){
                         echo $result['q_id'];
                       }
      

      检查http://www.w3schools.com/php/php_arrays_multi.asp 数组 => 键 => 值

                     // if its json
      
                       $results = json_decode(dbresult);
                        foreach($results as $result){
                         echo $result['q_id'];
                       }
      

      【讨论】:

      • 您可能应该找到比 W3Schools 更好的来源。
      • 他们不是最好的,但他们有基础,有什么问题?
      • 因为这是一个提问的网站(不是学习编程),我建议使用类似 Mozilla Web API 的东西。
      猜你喜欢
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2017-05-28
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多