2015-10-13 21:22
浏览 68

PHP / MYSQL / JSON:在IOS应用程序中调整解析api api

I am trying to follow this tutorial to parse a JSON web service for display in an app.

The tutorial takes the input for loans from a service called KIVA as follows:

{"loans":[{"id":961236,"name":"Alhassan","description":{"languages":["en"]},"status":"fundraising","funded_amount":0,"basket_amount":0,"image":{"id":1960690,"template_id":1},"activity":"Personal Purchases","sector":"Personal Use","themes":["Rural Exclusion"],"use":"to buy food and clothing, eliminating pressure to sell maize for low prices at harvest","location":{"country_code":"NG","country":"Nigeria","town":"Kaduna","geo":{"level":"town","pairs":"10 8","type":"point"}},"partner_id":288,"posted_date":"2015-10-13T21:00:03Z","planned_expiration_date":"2015-11-12T21:00:03Z","loan_amount":200,"borrower_count":1,"lender_count":0,"bonus_credit_eligibility":false,"tags":[]}

The current output from my service is:

[{"userid":"1","shcom":"hello","lcom":"hello there friend"}]

The main thing I think my web service is missing relative to the one above is:

"loans":`[ at the beginning of the output.

So I think what I need to do is insert that at the beginning of the JSON output as the objective-c code in the app uses "loans" to create an array as follows:

  NSArray* latestLoans = [json objectForKey:@"loans"]; // gets JSON

    NSLog(@"loans: %@", latestLoans); // Prints it

What would I need to do to my PHP code to print out JSON as in the tutorial?

Thanks for any suggestions:



while($row = mysql_fetch_assoc($res)) {
    $loans[] = $row;

图片转代码服务由CSDN问答提供 功能建议

我试图关注这个教程解析 JSON web服务以在应用程序中显示。

本教程从 KIVA 服务获取贷款的输入如下:

  {“loan  “:[{” ID “:961236”,名字 “:” Alhassan “ ”描述“:{ ”语言“:[ ”EN“]} ”状态“: ”筹款“, ”funded_amount“:0,” basket_amount  “:0,”image“:{”id“:1960690,”template_id“:1},”活动“:”个人购买“,”部门“:”个人使用“,”主题“:[”农村排斥“]  ,“使用”:“购买食品和服装,消除在收获时以低价出售玉米的压力”,“地点”:{“country_code”:“NG”,“country”:“Nigeria”,“town”:“  Kaduna“,”geo“:{”level“:”town“,”pair“:”10 8“,”type“:”point“}},”partner_id“:288,”posted_date“:”2015-10-  13T21:00:03Z”, “planned_expiration_date”: “2015-11-12T21:00:03Z”, “loan_amount”:200, “borrower_count”:1, “lender_count”:0 “bonus_credit_eligibility”:假, “标签”  :[]} 


  [{“userid”:“  1“,”shcom“:”你好“,”lcom“:”你好朋友“}] 

我觉得我的网络服务主要是mi 相对于上面的那个是:


所以我认为我需要什么 do是在 JSON 输出的开头插入,因为应用程序中的 objective-c 代码使用“loan”创建 array as 如下:

  NSArray * latestLoans = [json objectForKey:@“loans”];  //获取JSON 
 NSLog(@“loan:%@”,latestLoans);  //打印它




while($ row = mysql_fetch_assoc($ res)){  
 $ loans [] = $ row; 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxu3732 2015-10-14 23:33

    The answer here was to change

    $loans[] = array('$loan);
    $loans[] = array('loans'=>$loan);

    This inserts the 'loans' at the beginning

    点赞 打赏 评论
  • drzb7969753 2015-10-14 23:41

    It is a JSON object of arrays, you need to adjust like the following:

    $loans = array();
    while($row = mysql_fetch_assoc($res)) {
        $loans['loans'][] = $row;
    echo json_encode($loans);


    • The JSON needs to be valid, the one you posted isn't
    • Use PDO if you can, mysql_ is deprecated

    example in PDO:

    echo json_encode(array('loans'=>$stmt->fetchAll()));
    点赞 打赏 评论

相关推荐 更多相似问题