Converting arrays in Swift into properly formatted JSON documents

I have encountered an obstacle in my iOS app where I need to store two arrays in a remote MySQL database using PHP and JSON. Despite multiple days of effort, I have been unsuccessful in converting the arrays from the iOS app into JSON code without causing the app to crash. The arrays are being populated by a QR code reader and an input field, with each array containing an equal number of items.

The current code snippet generates the following JSON:

json string = {"b":"[\n\n]","p":"[\n\n]"}

Regardless of the changes I make, the app consistently crashes with errors such as 'Invalid top-level type in JSON write' or 'JSON text did not start with array or object'. Here is a version of the code I have been working on:

var productArray = [String]()
var amountArray = [String]()

In this particular case, I am attempting to convert the arrays to JSON data and then back to strings for debugging purposes. Subsequently, I intend to cast them as dictionaries before serializing them and sending the JSON data via a POST request to a specified URL.

Answer №1

In my opinion, the optimal approach is to utilize SwiftyJSON

By doing so, when you have a string, you can simply do

var json = JSON.parse(yourString)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Split a string using two different delimiters to generate a multidimensional array

I have a string with specific data elements: 01;Tommy;32;Coder&&02;Annie;20;Seller I am looking to format it into an array like this: array (size=2) 0 => array (size=4) 0 => string '01' (length=2) 1 => string 'To ...

I am facing difficulties accessing external URLs in a webview within my Ionic Android application

Recently, I encountered an issue with my Ionic app where it opens an external URL in a webview. The app worked perfectly fine when tested on the iOS simulator, but when testing it on the Android simulator, it failed to function correctly. Upon checking the ...

Improved methods for extracting nested JSON data from a Spark table

As a newcomer to Spark and Scala, I am facing the challenge of parsing a Nested JSON format column from a Spark Table. Let me show you an example row from the table: doc.show(1) doc_content object_id object_version {&quo ...

Having difficulty organizing the Dgrid

var CustomGrid = declare([Grid, Keyboard, Selection]); var questionGrid = new CustomGrid({ store: questionCacheStore, columns: [ editor({ ...

Sort through location pins with spinner icon

I have a spinner feature that enables me to choose between Car Center and Truck Center as the company type. Using data retrieved from JSON, I am displaying addresses on a map with markers in an AsyncTask. Initially, when I implemented the code without the ...

`I'm experiencing difficulty sending JSON data to Typeahead using PHP`

I am having trouble passing an array of data from PHP to typeahead. I have tried various solutions but nothing seems to work. When I display the response on the console, it shows the array of strings but they are not populating in the typeahead field. PHP ...

Steps for utilizing JsonConvert.DeserializeObject for nameless values:1. Create a class to

I came across a JSON string that looks like this: {"status":false,"data":{"errors":{"":"error45"}}} It's challenging to create a class for this JSON string because the last part doesn't have a na ...

SharePoint 2013 on a mobile device does not support scrolling within iframes

Recently, I set up a SharePoint 2013 website with a few iframes. However, when I access the site on my mobile devices (iPad and Android), I am unable to scroll through the entire page by touching within the iframe. I attempted to solve this issue by inclu ...

Obtain numerous JSON pages using jq

When making an API call, I receive a JSON response that can be formatted nicely using curl "https://mywebsite.com/api/cars.json&page=1" | jq '.' Displayed below is a sample of the output. Since there are multiple pages of data (112 total ...

Sending a variable to a function in JavaScript (winJs) from a different function

Greetings! Currently, I am developing a Windows 8 app using Java Script. function fetchFromLiveProvider(currentList, globalList,value) { feedburnerUrl = currentList.url, feedUrl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&outpu ...

The process of extracting and using JSON data from an existing MySQL database with PHP

My database table used to have a column called data containing JSON data. | data | - `"{"display":{"pageName":"TEBT Home","Name":"rtryrtyrty rtyrtyrtyrty", "Email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail= ...

Combine theme configuration options within Material-UI

I am interested in setting up custom theme rules in Material-UI. My goal is to create both light and dark themes and extend them with some shared settings. My initial idea was to store the common settings for the light and dark themes in a separate variab ...

What is the best way to generate a fresh JSON object within a react hook function?

I am currently facing two issues. Firstly, I am having trouble figuring out how to add/update the JSON items within a hook. Secondly, React seems to be restricting me from using the name that is stored in a previous JSON file. I am open to exploring alter ...

Send the output of MPDF back to the browser by utilizing JSON in combination with ExtJS

I am currently using mpdf to generate a PDF report in my PHP code. I have successfully been able to save the PDF file and view it by using Output($pdfFilePath), but now I need to send it back to the browser using JSON without saving it on the server. To ac ...

Exploring Angular 5's *ngFor directive with an array of objects

Here is the data I am working with: Initial set of data: var input = [ {ru: "R201", area: "211", unit: "211"}, {ru: "R201", area: "212", unit: "NONE"}, {ru: "R201", area: "HCC", unit: "NONE"}]; Desired result data: var result = [ {area: ...

To validate any object, ensure that it contains a specific key before retrieving the corresponding value in typescript

When looking at a random object, my goal is to verify that it follows a certain structure. obj = {WHERE:{antherObject},OPTIONS{anotherObject}} Once I confirm the object has the key using hasProperty(key), how can I retrieve the value of the key? I thoug ...

Explore ways to incorporate special symbols in a jQuery array

I'm looking to include special characters in a jQuery array. I'm not quite sure how to do this. Currently, my code is: $scope.categories = ['Red', 'White', 'Rose', 'Sparkling']; and I would like it to be: ...

Angular update row and save data to an array

When comparing the data of an edited row with the row just below it, I encounter a specific scenario. In a table containing 5 rows, as I edit records from top to bottom using the provided code snippet, I am able to store the values in an array. The most re ...

Utilizing Rust with Postgres JSON attribute presents a challenge when converting between the Rust `alloc::string::String` type and the Postgres `jsonb` type

At present, I am utilizing the code below. However, I aim to avoid casting my JSON to text in my postgres query as it introduces latency. async fn filter_data(min: f32, max: f32, pool: &Pool) -> Result<String, PoolError> { let client: Client = ...

Guide on extracting a single key and transforming it into a class with the help of GSON

Below is the JSON data: { "timestamp":"04295d4f-2a6f-4a38-a818-52108cbdc358", "lastFullSyncDate":null, "ftpInfo":null, "listingInfo":{ "itemID":"110179365615", "itemTitle":"test", "itemPrice":"88.223529411 ...