dsapkqaduj6718493 2011-05-26 18:55 采纳率: 100%
浏览 37

JSONkit,MySQL,iOS - 怎么样?

I've been searching and can't find out how to grab data from a MySQL database using JSON in iOS. Of course this will require a PHP middle-man, but how to do all of this seems unsearchable at this point.

Please could someone help?

Thank you!

  • 写回答

1条回答 默认 最新

  • doutuosai3504 2011-05-26 18:59
    关注

    depending on your php version you can json_encode() the results of your MySQL query, or (as in the below) you can hand craft the JSON.

    then in your ios, use a parser (I use SBJSON) and it will convert the JSON into an array or dictionary (depending on how you use it)...I haven't used JSONKit so can't comment on that aspect

    EDIT: Oh, actually, is that the 'direction' you meant (receive JSON)? or did you want to submit JSON from iOS to initiate the query?

    <?php
    //connect to DB       
    $query = "SELECT * FROM table_name"; 
    $result = mysql_query("$query");
    echo "{\"results\":";
    if($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    echo "[";
        echo "{\"field_one\":\"".$row["field_one"]."\",";
        echo "\"field_two\":\"".$row["field_two"]."\",";
        echo "\"field_three\":\"".$row["field_three"]."\",";
        echo "\"field_four\":\"".$row["field_four"]."\"";
        echo "}";
    }
    else{
    echo "\"no\"}";
    exit;
    }
    while($row = mysql_fetch_array($data,MYSQL_ASSOC)){
        echo ",{\"field_one\":\"".$row["field_one"]."\",";
        echo "\"field_two\":\"".$row["field_two"]."\",";
        echo "\"field_three\":\"".$row["field_three"]."\",";
        echo "\"field_four\":\"".$row["field_four"]."\"";
        echo "}";
    }
    echo "]}";
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据