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

8
  • 1
    check typeof Objected and typeof Raw Commented Feb 4, 2018 at 7:30
  • Do those console.log results really look the same? What browser are you using? Commented Feb 4, 2018 at 7:33
  • 1
    JSON.stringify: It converts JavaScript values to JSON. Commented Feb 4, 2018 at 7: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 Commented Feb 4, 2018 at 7:36
  • 1
    JSON is a String you have to parse it again. Your question makes no sense. See the duplicate. Commented Feb 4, 2018 at 7:37

2 Answers 2

4

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;
Sign up to request clarification or add additional context in comments.

6 Comments

JavaScript - Object - Notation -_-
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"
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.
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!
|
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);

1 Comment

i ask what is the Use of .Stringify() f

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.