Search code examples
iosobjective-chtmljson-ld

How to read JSON-LD data from HTML in Objective-C?


I am creating one app that downloads HTML file from server. I am storing that file in local storage for offline usage. I have requirement to parse data from that file. I can do it by parsing the HTML tags, but I want to make is standard way hence JSON-LD is a good way to do it using HTML5. So anytime the HTML structure changes, I would not have to update code.

I want to read JSON-LD data from HTML file in Objective-C. I googled it but not found any pretty way to read it.

Should I have to read it same way like reading HTML string from file? Can anyone please guide me, if there is any specific way to do it?


Solution

  • I have parsed JSON-LD model from HTML file using following way,

    1. Get Any XMLParser/HTMLParser Dictionary. (I used https://github.com/nicklockwood/XMLDictionary).

    2. converted content of HTML file into NSDictionary using XMLParser dictionary.

      var maindic = NSDictionary(XMLFile:NSBundle.mainBundle().pathForResource("JSON-LD_Sample_Card", ofType: "html"))

    Then from dictionary we can easily get array of different tags and there we can parse JSON-LD script Tag too.