Solved

Parsing json

Posted on 2013-11-05
6
379 Views
Last Modified: 2013-11-05
Hi,

I have a service call in my application which returns jsaon data. The call is like so:

            $.ajax({
                type: "GET",
                dataType: "json",
                url: 'http://rest.smartdoc.se/api/users/' + username + '/' + password,
                success: function (Data) {
                    var obj = $.parseJSON(Data);
                    alert(obj.FullName);
                }

            });

Open in new window


The service returns this:

{"Data":[{"FullName":"Doesn't exist","username":"sdf","password":"sdf"}],"Errors":null}

Open in new window


There obviously is something wrong because it is not working. Please help me.

Best regards
RTSol
0
Comment
Question by:RTSol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39623947
why :
url: 'http://rest.smartdoc.se/api/users/' + username + '/' + password,

and not :
url: '/api/users/' + username + '/' + password,
0
 

Author Comment

by:RTSol
ID: 39624012
Hi,

The application is running on a smartphone. I need to provide the complete url to the service which is running on a web server. The call to the service works fine and it returns the desired json. My problem is just to parse it.

-RTSol
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39624016
Look at your JSON, you probably need this:

var obj = $.parseJSON(Data);
alert(obj.Data.FullName);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39624025
Well... I looked at your code and really bad is this line:

'http://rest.smartdoc.se/api/users/' + username + '/' + password

You're passing username and password in clear text and not even https?
And I hope you're not doing this for each request...
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39624027
Actually, no - you need this:

success: function (Data) {
    var person = Data.Data[0]; //grab the first person from the Data array
    alert(person.FullName);
}

Open in new window

The Data returned contains a Data key, holding an array or people. If you are likely to be passing back more than one person then you'll need to loop through Data.Data
0
 

Author Closing Comment

by:RTSol
ID: 39624042
Thanks a lot - that is what I needed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question