how can i replace json string? - iphone

i want to replace particular data's in json string

Parse it, make the changes you need, then serialize it back out. You didn't say what JSON library you're using, but it should be fairly easy.

Without knowing the specifics of your problem:
[jsonString stringByReplacingOccurrencesOfString:#"replaceme" withString:#"replacement"];
But you're probably far better of using a JSON library.

Related

Identifying and formatting XML String to readable format in XMLParser

I am working in Swift and I have a set of Data that I can encode as a String like this:
<CONTAINER><Creator type="NSNull"/><Category type="NSNull"/><UMID type="NSArray"><CHILD>d1980b265cbd415c90f5d5f04efcb5df</CHILD><CHILD>7e0252c137c249fc92bd0f844effe27f</CHILD></UMID><Channels type="NSNumber">1</Channels></CONTAINER>
I am looking for a way to format this string as XML with indents so I can use XMLParser to properly read through it, which it currently does not. I imagine NSNull is when the object is empty, I just haven't seen this format so I don't know what to search for. Could it be closer to a Dictionary object? If so I'd be happy to format it as that as well.
I've also tried to create a XMLDocument from the data, but it doesn't fix the format.
EDIT:
I wanted to add a bit more information to help clarify what I am trying to do. This string above is derived from an encrypted piece of metadata from a file. In my code I identify the chunk of data that is encrypted, then decrypt it, and then convert that data to a string. It's worth noting that the string ends up having null characters in between each valid character, but I strip those out and end up with this string.
Copying this string into an XML Validator confirms it is valid XML. What is confusing to me is it's format, in which it has Object types such as NSNull and NSNumber. My post was originally hoping to identify this type of format. It seems like more than just XML.
In response to some of the comments, I have used XML Parser delegate with other XML strings and have a basic understanding of how it works. I should have originally mentioned that and instead said that XML Parser does not recognize any of these elements or strings within them.
UPDATE:
The issue ended up being the null characters in between each valid character. Stripping those out and then running it through XML Parser worked great. Thanks all.

How would I convert a valid Javascript JSON into a dictionary in Swift 2?

I am trying to create a dictionary from this JSON in an variable of type [String: AnyObject].
I am using Alamofire to make the request. However, the responseJSON response handler doesn't work since it is not a 'valid' JSON object in Swift. How can I go about tackling this?
Your text is not valid JSON (you can check this here), as it's missing quotation marks around attribute strings. While it might be a JavaScript object, that's not synonymous with valid JSON. NSJSONSerialization (which is surely what's backing that function) will correctly reject the input.
You should fix your JSON - preferably at the source. You could do it by post-processing with string editing functions in Swift, but this is a bad idea.

Converting Objects to plist file then to data

I have an array of objects that consist of several strings, I need to push these up to a web service as XML data. What are the steps involved here? As far as I know I need to convert the objects to a plist file, then convert this file to NSData (?) I can't find anything online that really lays it out..
Any help would be greatly appreciated.
Another approach is JSON. SBJSON is very common and simple to use.
It's a two-liner to get a json string from your array. Then you'll create an NSURLRequest that represents the post and an NSURLConnection that performs the request. Lot's of resources for that on SO and elsewhere.
You can try this or this. I doubt plist files are what you want. All you need is to serialize your object into the xml format your web service requires.

Rubymotion Base64

I am looking for a simple way to encode a string using base64. In ruby motion I can't just use the Base64encode of Ruby because I am not able to require it. So I thought I could use a build in function of Cocoa. But Cocoa does not seem to have a Base64encode function. I have found some categories on NSData, but don't know how to use them in a ruby motion project. Should I create a statics library for this?
I have got the feeling that I am looking in the wrong direction, there must be easy solution for this?
If you look at the source for Base64.encode64 method, you'll see that it just uses the pack method. So you can encode/decode like this (note that you need to put the thing you want to encode inside an array):
["my string"].pack("m")
# => "bXkgc3RyaW5n\n"
"bXkgc3RyaW5n\n".unpack("m").first
# => "my string"
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html#method-i-encode64

Parse JSON array

I fetch a JSON array from a web service with touchJSON.
Which looks like this:
[{"icecream": {"title": "Banana"}}, {"icecream": {"title": "Strawberry"}}]
I'm not able to parse this into a NSDictionary, because
touchJSON doesn't support JSON arrays.
How do I get my JSON array into a NSDicitionary?
Regards
Have you consider trying another framework? This one seems to support JSON arrays.
Maybe you could use another of the many JSON implementations listed on the JSON homepage.
You can chekc out the JSON webpage, where they provide links to parsing code in dozens of languages. However, at first glance it looks like you're trying to munge from one type of object (the JSON Array) into another that might not be able to capture all the relationships (the NSDictionary). Full disclaimer: I've never used an NSDictionary before.