如何在 iOS 中动态解析 JSON [英] How to parse JSON dynamically in iOS

查看:31
本文介绍了如何在 iOS 中动态解析 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用了第三方服务,它提供了一个 JS 文件.js文件发起一个http请求并得到一个json.我们解析了json并得到了我们想要的内容,但是json格式总是在变化.

We used a third party service and it provides a JS file. The js file launches an http request and get a json.We parsed the json and got the content we wanted but the json format always changes.

有没有办法解析json但不更新我们的应用程序?

Is there a way to parse the json but do not update our app?

推荐答案

不断更改架构听起来很愚蠢,但无论如何,也许您可​​以尝试在云中某处创建一个清单,将最新的架构关键字转换为您的应用程序明白吗?

It sounds awful stupid to constantly change schemas, but anyway, maybe you could try having a manifest somewhere in the cloud that translates the latest schema keywords into one your app understands?

基本上,我认为 JSON 中的信息是相似的(否则根本没有意义),只有关键字发生了变化.您可以拥有一个不断更新的 JSON,将应用中使用的关键字转换为网络服务使用的最新关键字.

Basically, I presume that the info in the JSON is similar (otherwise it wouldn't make sense at all) and only the keywords change. You could have a JSON you constantly update that translates the keywords used in the app into the newest one used by the webservice.

所以一个例子看起来像这样.想象一下,这是您在开发应用时习惯的格式(这是一个应用所期望的).

So an example would look like this. Imagine this is the format you are used to when developing the app (this is the one app expects).

{
    "name" : "Henri",
    "title" : "iOS Developer"
}

现在,如果网络服务更改了它的架构并返回类似这样的内容

Now if the webservice changes it's schema and returns something like this

{
    "key1" : "Henri",
    "key2" : "iOS Developer"
}

你应该有一个 manifest.json 像这样翻译它

You should have a manifest.json which translates it like this

{
    "name" : "key1",
    "title" : "key2"
}

我希望你能明白我的意思,基本上你可以将翻译转移到云端,让你有机会在应用程序保持不变的情况下保持最新状态.所以在翻译加载后你可以像这样访问数据

I hope you get where I'm going with this, basically you can shift the translation to the cloud, giving you the chance to keep it up to date while app remains the same. So after loading in the translation you can access the data like this

NSString *name = [actualJSON objectForKey: [manifestJSON objectForKey: @"name"]];

这篇关于如何在 iOS 中动态解析 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆