Tampilkan postingan dengan label JSON. Tampilkan semua postingan
Tampilkan postingan dengan label JSON. Tampilkan semua postingan

Kamis, 14 Desember 2023

javascript forEach on json object

 For everyone who is fucked up with forEach on JSON object. 

Find solution in this link

https://codedamn.com/news/javascript/how-to-fix-typeerror-foreach-is-not-a-function-in-javascript

or something like this

 

                        var myval = JSON.parse(results);


                        Object.entries(myval).forEach(entry => {
                            [key, value] = entry;
                            console.log(entry);
                        });

Kamis, 09 April 2020

Creating JSON data in Javascript

Quick post about how to create JSON data in javascript.
I want to POST some inputs via AJAX but I want send them just in one variable, don't ask me why. I just want to do it that way.

here is my HTML code

<input id="name" type="text">

<input id="phone" type="text">

<input id="address" type="text">

I want send them as JSON data, here is how I do it:


var obj = new Object();
obj.name = document.getElementById('name').value;
obj.phone= document.getElementById('phone').value;
obj.address= document.getElementById('address').value;

var myData = JSON.stringify(obj);


That's it. the variable myData is the json data.

Rabu, 04 September 2019

2 Dimensional Array to JSON data in Javascript

Quick posting on how to convert 2 dimensional javascript array to JSON data. Source!
  
var questions = [];
 
 for(i=0;i<3;i++) {
    questions[i] = {};
    questions[i]["question"] = "hey";
    questions[i]["rating"] = "123";
 });


var encoded = JSON.stringify(questions);
console.log(encoded);

Take a look at the third code line! With this way, you send the second array dimension as array object. If you send this data with AJAX to a PHP code, you should put 'true' as the second argument on json_decode php function. It will convert the JSON data into a php array.
  
<?php

$myarray = json_decode(encoded, true);

?>

Senin, 17 Juni 2019

Unexpected end of json input in Node.js

Again, something that I have to keep in my blog.

I am developing a REST API, that decrypts an array of encrypted text. I post the array data in form of JSON data and nodejs keeps giving me an error "unexpected end of json input". But if I check the JSON data, everything looks fine.

stackoverflow saves my live again.

The explanation and the solution is shown in that link. Check the solution using "chunk"