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

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.

Related

What's the datatype of the value returned by octokit.createPullRequest() in octokit-plugin-create-pull-request?

I want to make a pull request from a react-native app. I am using the octokit-plugin-create-pull-request library from here. They have a function octokit.createPullRequest() in their documentation. I am at a point where I need to know the datatype of the value it returns. Is it an array or an object? Or is it another datatype?
It returns a pull request object, with the properties you see in the example response https://docs.github.com/en/rest/reference/pulls#create-a-pull-request.

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.

Converting a String to a Splittable in GWT

I'm maintaining a site written in GWT (2.5.0) that is used internally by our development team, and I've been experimenting with using AutoBeans for client side json parsing. I have a few objects with json that is not well defined — a developer can dump whatever json string he wants in there — so I'm using a Splittable property. In order to support editing this arbitrary json I'd like to convert a String into a Splittable, but I haven't found a straight-forward way of accomplishing this. Do I need to implement this interface myself or resort to something hacky like wrapping the json in another json object I can then decode into a throw-away AutoBean just to get a Splittable of the original json?
StringQuoter is the utility class which we do much of our manual Splittable work with.
Just user StringQuoter.create("some string"); to produce a Splittable whose payload is
"some string"
Once you have that splittable, you can assign it to a key in another splittable with the following method:
Splittable.assign(Splittable parent, String propertyName);
However, if you are trying to convert some arbitrary string which contains a JSON structure into a splittable, use StringQuoter.split(..) to create it. The resulting splittable can be queried as normal (i.e. what keys exist/don't exist, etc).

how can i replace json string?

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.

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.