0

I want to parse this using jQuery or javascript My JSON generated from the PHP code is as follows:

JSON

{
 "user": {
 "name": "John Carter",
 "position": 0,
 "tickets": {
 "months": [
 "October",
 "November"
 ],
 "start_Time": "2014-10-02",
 "end_Time": "2014-11-21",
 "Open": [
 "1",
 "3"
 ]
 }
 }
}

My Javascript

$.ajax({
 url: 'ajax.report.php',
 type: 'POST',
 data: 'start='+startDate+'&&end='+endDate,
 success: function(response){
 var json_obj = $.parseJSON(response);
 for(var i =0; i < json_obj.user.length; i++){
 //What is the next?
 }
 }
 });

Kindly help. Thank you !

asked Oct 4, 2014 at 8:17
1
  • Hi @user3312993 if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. Commented Oct 5, 2014 at 6:19

3 Answers 3

1

The json returned by jQuery is already a JavaScript object, not a string. You do not have to parse it any further to use it. I'm on a mobile device right now so I can't confirm, but I'm pretty sure you can just do this:

success: function(response){
 //try
 var name = response.user.name
 //try this as well
 var name = response.name
}

You should be able to print out the name string using console.log() after this.

answered Oct 4, 2014 at 8:37

Comments

0

I'm assuming that you're trying to convert some JSON in string form to an object usable by JS. You can parse a valid JSON string into a JS object by using JSON.parse(jsonString). (Where jsonString is a variable containing the string of JSON)

answered Oct 4, 2014 at 8:22

Comments

0

I suggest that you look over the jquery docs for more info, the best thing you can use is jquery.ajax Make sure that in your php script you encode the text as JSON, otherwise jquery will interpret it as plain/text.

Little snippet, which you can also find on the jquery docs..

$.ajax( "example.php" )
.done(function(data) {
 console.log(data);
});
answered Oct 4, 2014 at 8:23

2 Comments

Yes but i dont know how to get the value of json object with array.
It's fetched as object, meaning you can use data.ARRAYKEY to get what you want. Unless the key is an increment value, then you need to use data[KEY]..

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.