$.ajax({ url: 'http://localhost:8080/order', type: 'DELETE', dataType: 'json', data: productID, processData: false, headers: {'Content-Type': 'application/json'}, complete: function(products) { const response = JSON.parse(JSON.stringify(products)); const productsMap = new Map(Object.entries(response.responseJSON)); productsMap.forEach((value, key) => { let keyObj = JSON.parse(key); console.log(keyObj); }); } });
I implement an algorithm for removing goods from the order basket, the server deletes the selected product from the database and returns the updated list of products in the form of Map<Product, Integer>
where Product is the product itself and Integer is its quantity. On the client, response.responseJSON is displayed as "Product(productID=24047, category=electronics, subCategory=tv, type=tv, brand=BBK, model=40LEX-5056/FT2C, country=Китай, price=23995, orders=[])": 1
You need to pull out the key Product as a JS object and not as a string, JSON.parse throws a SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
error SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Question: How to parse the key with the Product object as a JS object?
PS if sending a List instead of a Map from the server, Product parses into a JS object without problems, but in this case it’s impossible to transfer the quantity of goods stored in the database ...