从 json 字符串获取媒体 url

发布于 2024-10-18 04:56:48 字数 1007 浏览 3 评论 0原文

我如何从这个 json 中获取图像路径+名称?

    /*
     {
     distance = 0;
     "effective_from" = "2011-02-19 12:27:20";
     "effective_thru" = "2011-02-19 13:27:20";
     fname = eric;
     latitude = "11.92179";
     "listing_id" = 60;
     lname = cartman;
     longitude = "-74.70189";
     lstatus = active;
     media = "{\"media\":
     [
     {\"id\":149,\"media_url\":\"mediauploads/149.jpg\"},
     {\"id\":150,\"media_url\":\"mediauploads/150.jpg\"},
     {\"id\":151,\"media_url\":\"mediauploads/151.jpg\"},
     {\"id\":152,\"media_url\":\"mediauploads/152.jpg\"}
     ]}";
     tags = "Separate multiple tags with commas";
     text = "Listing text";
     title = "Please provide a descriptive title";
     username = eric;
     "users_id" = 13;
     },
     */

我的问题是 NSDictionary *media = [userContent objectForKey:@"media"];返回 -[NSCFString objectForKey:]: 发送到实例 0x66662a0 的无法识别的选择器,因此我无法从中获取数组。

更新:事实证明我从服务器获取了字符串。我怎样才能将它解析到字典中?

任何帮助表示赞赏。

谢谢!

How would I get the image path+name out of this json?

    /*
     {
     distance = 0;
     "effective_from" = "2011-02-19 12:27:20";
     "effective_thru" = "2011-02-19 13:27:20";
     fname = eric;
     latitude = "11.92179";
     "listing_id" = 60;
     lname = cartman;
     longitude = "-74.70189";
     lstatus = active;
     media = "{\"media\":
     [
     {\"id\":149,\"media_url\":\"mediauploads/149.jpg\"},
     {\"id\":150,\"media_url\":\"mediauploads/150.jpg\"},
     {\"id\":151,\"media_url\":\"mediauploads/151.jpg\"},
     {\"id\":152,\"media_url\":\"mediauploads/152.jpg\"}
     ]}";
     tags = "Separate multiple tags with commas";
     text = "Listing text";
     title = "Please provide a descriptive title";
     username = eric;
     "users_id" = 13;
     },
     */

My problem is that NSDictionary *media = [userContent objectForKey:@"media"]; returns a -[NSCFString objectForKey:]: unrecognized selector sent to instance 0x66662a0 so I cannot get the Array out of it.

Update: so it turns out that I get as a string from the server. How can I parse it to a dictionary?

Any help is appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

虚拟世界 2024-10-25 04:56:48

TouchJSON 提供了一个 JSON Deserializer 类,其中包含将 JSON 转换为字典的方法。

如果您以字符串形式返回 JSON,请首先将其转换为 NSData。例如:

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];

然后创建一个 CJSONDeserializer 并传递数据以返回字典:

CJSONDeserializer *deserializer = [CJSONDeserializer deserializer];
error = nil;
NSDictionary *dict = [deserializer deserializeAsDictionary:jsonData error:&error];

然后您应该能够从字典中提取所需的数据。

TouchJSON provides a JSON Deserializer class that contains a method to convert JSON to a dictionary.

If you get the JSON back as a string, firstly convert it to NSData. For example:

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];

Then create a CJSONDeserializer and pass the data to return a dictionary:

CJSONDeserializer *deserializer = [CJSONDeserializer deserializer];
error = nil;
NSDictionary *dict = [deserializer deserializeAsDictionary:jsonData error:&error];

You should then be able to extract your required data from the dictionary.

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