How to parse JSON dynamically in iOS

后端 未结 3 509
遇见更好的自我
遇见更好的自我 2021-01-25 07:32

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

3条回答
  •  庸人自扰
    2021-01-25 08:26

    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?

    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"
    }
    

    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"]];
    

提交回复
热议问题