Objective C - 数据绑定?

发布于 2024-11-02 20:13:34 字数 1003 浏览 1 评论 0原文

我正在尝试创建一个将 JSON 字符串转换为对象的 JSON 库。 标记与 JSON 字符串相关的属性的最简洁方法是什么? 是否可以实现类似下面的代码的功能

注意:下面的代码不起作用,它只是一个示例,用于展示我正在尝试实现的内容

JSON String

{
   "FIRST_NAME": "Some first name",
   "LAST_NAME":  "Some last name"
   "CLASSES" : 
   [
      {
         "CLASS_NAME": "class 1"
      }
      {
         "CLASS_NAME": "class 2"
      }
   ]
}

Model

@interFace Student

[JSON = "FIRST_NAME"]
@property (nonatomic, retain) NSString *firstName;

[JSON = "LAST_NAME"]
@property (nonatomic, retain) NSString *lastName;

[JSON = "CLASSES"]
@property (nonatomic, retain) NSArray  *classes;

@end

<强>JSON方法

@implementation JSON
+ (id)getObjectFromJSONString:(NSString*)string withType:(Class)class
{
   //Create a student Object
   //for each property if there is a JSON mark look for the value in json string
   //populate all available values
   //return object
}
@end

I am trying to create a JSON library that converts JSON string into objects.
What's the cleanest way to mark properties that are related to JSON string?
Is it possible to achieve something like the code below

NOTE: The code below doesn't work it's just a sample to show what I'm trying to achieve

JSON String

{
   "FIRST_NAME": "Some first name",
   "LAST_NAME":  "Some last name"
   "CLASSES" : 
   [
      {
         "CLASS_NAME": "class 1"
      }
      {
         "CLASS_NAME": "class 2"
      }
   ]
}

Model

@interFace Student

[JSON = "FIRST_NAME"]
@property (nonatomic, retain) NSString *firstName;

[JSON = "LAST_NAME"]
@property (nonatomic, retain) NSString *lastName;

[JSON = "CLASSES"]
@property (nonatomic, retain) NSArray  *classes;

@end

JSON Method

@implementation JSON
+ (id)getObjectFromJSONString:(NSString*)string withType:(Class)class
{
   //Create a student Object
   //for each property if there is a JSON mark look for the value in json string
   //populate all available values
   //return object
}
@end

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

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

发布评论

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

评论(3

最初的梦 2024-11-09 20:13:34

这当然是可能的。但是,请注意,Objective-C 不支持可以在运行时检查的任意源代码注释,因此 JSON 键和表示实例变量或声明属性的对象键之间的映射不会与您所描述的完全一样。

我建议您查看 RestKit。除了帮助您的程序连接到 RESTful 服务之外,它还具有对象映射基础设施,可以以声明方式将远程 JSON 消息转换为本地域对象。

It is certainly possible. However, note that Objective-C doesn’t support arbitrary source code annotations that can be inspected at runtime, so the mapping between a JSON key and an object key representing an instance variable or declared property won’t be exactly like you’ve described.

I recommend you take a look at RestKit. Besides helping with connecting your program to RESTful services, it has an object mapping infrastructure that turns remote JSON messages into local domain objects in a declarative manner.

晨曦÷微暖 2024-11-09 20:13:34

我最终为此特定目的编写了一个小型库。

https://github.com/aryaxt/OCMapper

I ended up writing a small library for this specific purpose.

https://github.com/aryaxt/OCMapper

ˉ厌 2024-11-09 20:13:34

我假设您正在使用 JSON-Framework 来解析 JSON。

据我所知还没有。我们一直在编写一些使用 JSON 输入的数据客户端。

根据维护 REST 服务的人员,您还必须检查 NULL 值等内容。此外,您可能必须将值转换为整数/浮点。

I assuming that you are using JSON-Framework to parse JSON.

Not as far as I know. We have been writing a few data clients that consume JSON input.

Depending on who maintains the REST service you also have to check for things like NULL values. Also you might have to covert values to Integer/Float.

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