2

Here i'm new to Jquery Please Help me

 var obj={"FirstName":'Hussain','LastName':'Ali','MiddleName':'Zain'}

    $('#jsonConvert').click(function () {

        var Objected = JSON.stringify(obj);
        console.log(Objected);
        var Raw = obj;
        console.log(Raw);
    })

When i Convert .Stringify() its Give me same result as of Raw Then what is use of .Stringify(); If i want Only FirstName then how can i get

Maistrenko Vitalii
  • 994
  • 1
  • 8
  • 16
Md Ghousemohi
  • 197
  • 2
  • 13
  • 1
    check `typeof Objected` and `typeof Raw` – Pranav C Balan Feb 04 '18 at 07:30
  • Do those console.log results really look the same? What browser are you using? – JJJ Feb 04 '18 at 07:33
  • 1
    [JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify): It converts JavaScript values to JSON. – Paul Feb 04 '18 at 07:34
  • When i convert its into Json Then how can i Get FirstName When i Use as var Objected = JSON.stringify(obj); console.log(Objected.FirstName); its gives me undefind – Md Ghousemohi Feb 04 '18 at 07:36
  • 1
    JSON is a `String` you have to parse it again. Your question makes no sense. See the duplicate. –  Feb 04 '18 at 07:37
  • @Roberson could u please give me any example their i can learn – Md Ghousemohi Feb 04 '18 at 07:38
  • @MdGhousemohi Just use `obj.FirstName`. Don't convert `obj` to JSON... – Paul Feb 04 '18 at 07:40
  • @MdGhousemohi [Here is an example](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – JJJ Feb 04 '18 at 07:58

2 Answers2

3

JSON.Stringify will convert your JSON object into string and notice the word object.

Meaning is you receive an object of json:

var obj = {
  "FirstName": 'Hussain',
  'LastName': 'Ali',
  'MiddleName': 'Zain'
};

And you want to send it to the server as string, you use:

JSON.stringify(obj);

The result is:

"{
      "FirstName": "Hussain",
      "LastName": "Ali",
      "MiddleName": "Zain"
    }"

The opposite is JSON.parse(), it will convert a valid JSON string into json object and notice the word valid JSON string. If you try to parse object to object, you will get an error "Unexpected type O at location zero" or any other random error of that sort. Meaning:

Meaning is you receive a string of json:

  var obj = "{
  "FirstName": "Hussain",
  "LastName": "Ali",
  "MiddleName": "Zain"
}"

And you use it by parsing it into object:

JSON.parse(obj).FirstName;
Barr J
  • 10,636
  • 1
  • 28
  • 46
  • 2
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas Feb 04 '18 at 08:54
  • JavaScript - Object - Notation -_- – Barr J Feb 04 '18 at 08:56
  • 2
    It's either a string (JSON) or a key-value-pair data structure (object). But not both at the same time, hence... There's no such thing as a "JSON object" – Andreas Feb 04 '18 at 08:59
  • A string is an object, pure and simple. – Barr J Feb 04 '18 at 08:59
  • 1
    people are mistaking string all the time, there is no such a thing string, there never was. A string is an array of chars. The string class just creates a wrapper so developers won't tear their hairs off when trying to write full string sentences (C language hmm...). A string is an object, reference type that behaves like a value type. – Barr J Feb 04 '18 at 09:05
  • 1
    HEY I got it, it was leaving the USER_ID param blank! The server needed a value and couldn't take a blank value. JEEZ, I can't believe it! – shadow2020 Dec 15 '21 at 23:39
0

To get FirstName, you only use obj.FirstName:

var obj = {
  "FirstName": 'Hussain',
  'LastName': 'Ali',
  'MiddleName': 'Zain'
};
console.log(obj.FirstName);
console.log(obj);
console.log(JSON.stringify(obj)); // this will give you a string that follows the JSON notations

The JSON.stringify method basically converts a JavaScript value into a JSON string. Typically used to convert JavaScript arrays or objects to JSON.

Example:

var someArray = ['Hello', 'World', 123, true];
var json = JSON.stringify(someArray);
console.log(json);
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35