Add/Edit JSON data iOS?

后端 未结 2 1853
抹茶落季
抹茶落季 2021-02-11 10:34

I am using the below code to fetch JSON data from my local drive. It works quite well

Now I want to add my JSON data to the Same URL

I don\'t w

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-11 11:05

    In my code I append JSON to an open file with the following (well I actually made a macro)

    I do it both with the serialization and just normal text I type to format it a little better:

    For normal text I made a macro to append:

    #define jsonAppend(X) [outStream write:[[@X dataUsingEncoding:NSUTF8StringEncoding] bytes] maxLength:[@X lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]
    

    -> you would have to open an output stream to your file and queue it up to the end I would suppose but in my code I just call

    jsonAppend("WhatEverIwAntToAppend");
    

    And as far as appending to an existing JSON structure:

    [NSJsonSerialization writeJSONObject:My-Dictionary-Object toStream:outStream options:1 error:&error]
    

    Again as long as you have a file handle it should be easy.

    With regards to how to open a stream in append mode check: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/nsoutputstream_class/reference/reference.html

提交回复
热议问题