在 iOS 上下载并解析 JSON 工具包

发布于 2024-12-07 06:23:31 字数 864 浏览 1 评论 0原文

我正在尝试将其从网络服务器解析为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼窥荷 2024-12-14 06:23:31

我真的很喜欢 JSONKit,因为它简单且速度快。你必须意识到,它返回的是作为对象的顶层构造 - 所以你必须考虑它。它将是一个 NSDictionary(因此您可以对其进行类型转换),并且它将有一个关键的“交易”。

这个键将返回一个 NSDictionary 对象的 NSArray,每个对象都有键 ID、描述和价格。

就像这样(例如,其中 itemListData 是从 URL 获取的 JSON 数据):

JSONDecoder *decoder = [JSONDecoder decoderWithParseOptions:JKParseOptionStrict];
NSData *immutableItemList = [itemListData copy];
NSArray *returnedData = (NSArray *) [[decoder objectWithData:immutableItemList] objectForKey:@"transactions"];

因此,我们获取原始 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):

JSONDecoder *decoder = [JSONDecoder decoderWithParseOptions:JKParseOptionStrict];
NSData *immutableItemList = [itemListData copy];
NSArray *returnedData = (NSArray *) [[decoder objectWithData:immutableItemList] objectForKey:@"transactions"];

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.)

黒涩兲箜 2024-12-14 06:23:31

嘿,伙计,我过去所做的就是去 facebook 并下载他们的开发者 sdk。那个sdk里有一个很棒的JSON解析工具。您可以在这里找到它 http://developers.facebook.com/docs/reference/iossdk/< /a>
然后,您只需将其添加到您的项目中,并在标头中执行一个操作

 #import "JSON.h"

,然后当您收到该字符串时,您就会说

NSDictionary *data = [my_data_string JSONValue];
NSArray *transactions = [data objectForKey:@"Transactions"];

这应该可以工作:)

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

 #import "JSON.h"

and then when you get that string you say

NSDictionary *data = [my_data_string JSONValue];
NSArray *transactions = [data objectForKey:@"Transactions"];

and that should work:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文