在 iOS 上下载并解析 JSON 工具包
我正在尝试将其从网络服务器解析为 iOS 中的表格视图,
{
"transactions": [
{
"ID": "350",
"description": "Macbook Pro 17 inch",
"price": "2811.83"
},
{
"ID": "351",
"description": "Macbook - white",
"price": "1720.10"
},
{
"ID": "352",
"description": "iPad 2",
"price": "650.00"
},
{
"ID": "353",
"description": "Macbook Pro 17 inch",
"price": "3233.98"
},
{
"ID": "354",
"description": "Macbook Pro 15 inch",
"price": "2100.55"
},
{
"ID": "355",
"description": "Macbook Air",
"price": "899.99"
},
{
"ID": "356",
"description": "Mac Pro",
"price": "3400.77"
}
]
}
我唯一想要的就是描述和价格。我需要将每件商品的价格相加并得出总计。每个事务名称需要存储在 NSArray
中,然后显示在 UITableView
中。
有什么帮助吗? JSONkit 没有给我 SBJSON 解析器
I am trying to parse this from a webserver to a tableview in iOS
{
"transactions": [
{
"ID": "350",
"description": "Macbook Pro 17 inch",
"price": "2811.83"
},
{
"ID": "351",
"description": "Macbook - white",
"price": "1720.10"
},
{
"ID": "352",
"description": "iPad 2",
"price": "650.00"
},
{
"ID": "353",
"description": "Macbook Pro 17 inch",
"price": "3233.98"
},
{
"ID": "354",
"description": "Macbook Pro 15 inch",
"price": "2100.55"
},
{
"ID": "355",
"description": "Macbook Air",
"price": "899.99"
},
{
"ID": "356",
"description": "Mac Pro",
"price": "3400.77"
}
]
}
The only things I want from this are the descriptions and the prices. I need to add up the price of each item and get a total. Each transaction name needs to be stored in an NSArray
and then displayed in a UITableView
.
Any help? The JSONkit doesnt give me the SBJSON parser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真的很喜欢 JSONKit,因为它简单且速度快。你必须意识到,它返回的是作为对象的顶层构造 - 所以你必须考虑它。它将是一个 NSDictionary(因此您可以对其进行类型转换),并且它将有一个关键的“交易”。
这个键将返回一个 NSDictionary 对象的 NSArray,每个对象都有键 ID、描述和价格。
就像这样(例如,其中
itemListData
是从 URL 获取的 JSON 数据):因此,我们获取原始 JSON 数据,实例化解码器,然后将其解码到字典中 - 并检索 1字典中的对象(在本例中为数组,因为它就是数组)。对于新手来说还不错吧?
(我应该补充一点 - 我制作 NSData 的不可变副本的原因是此代码片段处于异步下载中,在
connectionDidFinishLoading
方法中触发。)I really liked JSONKit because of its simplicity and speed. You have to realize, what it returns is the top level construct as an object - so you have to think about it. It will be a NSDictionary (so you can typecast it), and it will have one key "transactions".
This key will return an NSArray of NSDictionary objects, each of which will have the keys ID, description, and price.
So something like this (where
itemListData
is the JSON data fetched from the URL for example):So, we took the raw JSON data, instantiated a decoder, then decoded it into a dictionary - and retrieved the 1 object in the dictionary (as an array in this case, because that's what it is). Not too bad for a newbie, eh?
(I should add - the reason I make an immutable copy of the NSData, is that this code snippet is in an asynchronous download, triggered in the
connectionDidFinishLoading
method.)嘿,伙计,我过去所做的就是去 facebook 并下载他们的开发者 sdk。那个sdk里有一个很棒的JSON解析工具。您可以在这里找到它 http://developers.facebook.com/docs/reference/iossdk/< /a>
然后,您只需将其添加到您的项目中,并在标头中执行一个操作
,然后当您收到该字符串时,您就会说
这应该可以工作:)
Hey man so what I have done in the past is go to facebook and download their developer sdk. In that sdk is a great JSON parsing tool. You can find it here http://developers.facebook.com/docs/reference/iossdk/
Then you simply add that to your project and in you header do an
and then when you get that string you say
and that should work:)