如何从NSDictionary将json显示到uitextview中 [英] How to display json into uitextview from NSDictionary
问题描述
我现在整个周末都在尝试将简单的数据从mysql数据库返回到Json,再到我的应用程序中,再到漂亮的UItextview.
I'm trying now for the whole weekend to get simple data back from mysql database into Json into my app into a nice UItextview.
这是我的php代码:
this is my php code:
<?PHP
include("Connection.php");
$query="Select * from Api";
$result=mysql_query($query,$conn);
while ($row1 = mysql_fetch_array($result)){
$users[]=$row1;
}
$body = array();
if (!empty($users))
$body['users'] = $users;
if (!empty($body))
echo json_encode ($body);
include("dbClose.php");
?>
这是我的Xcode代码:
this is my Xcode code:
NSURL *url = [NSURL URLWithString:JSON];
NSLog(@"jsonreturn#########=%@",url);
NSError *error = nil;
NSData *jsondata = [NSData dataWithContentsOfURL:url];
NSDictionary *dic = nil;
if (!jsondata)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@"Data not found !!." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
// NSLog(@"data %@",data);
dic = [NSJSONSerialization JSONObjectWithData:jsondata options:0 error:&error];
[_userEmail removeAllObjects];
[_userFirst removeAllObjects];
NSLog(@"Dictionary data %@",dic);
for (NSDictionary *obj in [dic objectForKey:@"users"]) {
;
[_userEmail addObject:[obj valueForKey:@"Email"]];
}
user_mail.text = [NSString stringWithFormat:@"%@", [dic valueForKey: @"Email"]];
这是Xcode中的日志,您可以在这里看到 电子邮件 ,这样您就可以看到数据解析良好:
this is the log in Xcode you can see here the email so as you can see the data parsing is good:
Dictionary data {
users = (
{
0 = 1;
1 = "dani\t";
10 = "";
11 = "";
12 = 0;
2 = "l\f)\U008f\U00d0\U00b7`\U00fcR\U0092\U00bauU\U00f2]";
3 = dani;
4 = dani;
5 = "";
6 = "";
7 = "";
8 = dani;
9 = dani;
Country = "";
Email = "dani\t";
"First_Name" = dani;
这是电子邮件的应用程序 UItextview 结果:
and this is the app UItextview result for the email:
(null)
所以您可以看到我的问题在这里是如何从NSDictionary获取值以文本视图的形式显示在应用程序中,我不想使用表格视图或上面的代码不是我的代码,所以请坚持为此,任何帮助都将非常感谢.
so as you can see my problem here is how to i get the value from the NSDictionary to be displayed in the app as a textview I don't want to use table view or something not such my code above so, please stick to this and any help will be great thank you all.
这是非常基本的,我刚得到我的数据库,并想要选择一些代码并将该代码返回到我的应用程序中,并返回到UItextview中,这是非常不错的,任何对我有用的方法都向我展示如何做到这一点整个周末,我都在疯狂地寻找解决方案,而在json和textview的网络上却一无所有!.
It is very basic I just got my database and want to select some code and return that code into my app into a UItextview any other way to do so will be great, every way work for me just show me how to do this I got crazy over the weekend searching for a solution for this and nothing just nothing on the web for json and textview!.
推荐答案
如果您从日志中得到的输出是这样的:
If your output from the log is this:
Dictionary data {
users = (
{
0 = 1;
1 = "dani\t";
10 = "";
11 = "";
12 = 0;
2 = "l\f)\U008f\U00d0\U00b7`\U00fcR\U0092\U00bauU\U00f2]";
3 = dani;
4 = dani;
5 = "";
6 = "";
7 = "";
8 = dani;
9 = dani;
Country = "";
Email = "dani\t";
"First_Name" = dani;
}
)}
然后数据字典包含一个键为用户"的字典.
Then the dictionary of data contains a dictionary with key "users".
在用户"字典中,您有一个数组.在日志("等于数组中,"{"等于字典,表示json字符串.
Inside the "users" dictionary you have an array. In the log "(" equals array, "{" equals dictionary, for json strings.
在用户数组中,对象0有一个包含多个键的字典(0、1、10、11..Country,Email ...)
Inside the array of users, object 0 has a dictionary with several keys (0,1,10,11..Country,Email...)
因此要将键"Email"的值放在标签中,您可以执行以下操作:
So to put the value of the key "Email" in a label, you could do this:
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsondata options:0 error:&error];
NSArray *users = [dic objectForKey:@"users"];
NSDictionary *firstUser = [users objectAtIndex:0];
user_mail.text = [NSString stringWithFormat:@"%@", [firstUser valueForKey: @"Email"]];
您可以迭代该数组以对用户"数组中的所有对象执行此操作,或者,如果您想要第一个或最后一个对象,则可以执行NSDictionary * firstUser = [用户firstObject]或NSDictionary * firstUser = [用户lastObject].
You could iterate the array to go do this for all objects in the "users" array, or if you know you want the first or last, you could do NSDictionary *firstUser = [users firstObject] or NSDictionary *firstUser = [users lastObject].
我希望能帮上忙.
这篇关于如何从NSDictionary将json显示到uitextview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!