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:
Edit:
PHP:
while($row = mysql_fetch_assoc($res)) {
$loans[] = $row;
}
我试图关注这个教程解析 本教程从 我服务的当前输出是: p>
我觉得我的网络服务主要是mi 相对于上面的那个是: p>
“贷款”:`[在输出的开头。 p>
所以我认为我需要什么 do是在 我需要做什么才能打开我的PHP代码来打印出JSON教程? p>
感谢您的任何建议: p>
编辑: p>
JSON code>
web服务 code>以在应用程序中显示。 p>
KIVA code>服务获取贷款的输入如下: p>
{“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”:假, “标签” :[]}
code> pre>
[{“userid”:“ 1“,”shcom“:”你好“,”lcom“:”你好朋友“}]
code> pre>
JSON code>输出的开头插入,因为应用程序中的
objective-c code>代码使用“loan”创建
array code> as 如下: p>
NSArray * latestLoans = [json objectForKey:@“loans”]; //获取JSON
NSLog(@“loan:%@”,latestLoans); //打印它
code> pre>
PHP:
while($ row = mysql_fetch_assoc($ res)){
$ loans [] = $ row;
}
code> pre>
div>
The answer here was to change
$loans[] = array('$loan);
to:
$loans[] = array('loans'=>$loan);
This inserts the 'loans' at the beginning
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);
Note:
- 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()));