1
<script type="text/javascript" src="jquery.mobile/jquery-1.7.2.min"></script>
<script type="text/javascript" src="docs/assets/js/jquery.jsonp.js"></script>
<script> 
     $(document).ready(function(){
            var output = $('#output');
            $.ajax({
                url: 'http://musi.php/?oper=getds&dev_id=f587&cur_id=2',
                dataType: 'jsonp',
                jsonp: 'jsoncallback',
                timeout: 5000,
                success: function(data, status){
                    $.each(data, function(i,item){ 
                        var landmark ='<h1>'+item.Nick_Name+'</h1>';
                        output.append(landmark);
                    });
                },
                error: function(){
                    output.text('There was an error loading the data.');
                }
            });
        });
</script>

i have a url(for eg) and its json values as follows:

{"RetVal":"Ok","ValueRsp":[{"Feedback_Id":"22","Customer_Id":"543","Feedback_Type_Id":"1","First_Name":"Tester Tester","Last_Name":"NA","Nick_Name":"Xgcfyxfu"}]}

error: invalid label
Line 1 [Break On This Error] 
{"RetVal":"Ok","ValueRsp":[{"Feedback_Id":"22","Customer_Id":"543","Feedback_Typ...

i need to parse it and use it in my app. i am new to javascript. kindly help

8
  • What exactly is wrong with the code you have? Do you get any error? We cannot help you if we don't know what the problem is. Commented Jan 21, 2013 at 11:40
  • invalid label {"RetVal":"Ok","ValueRsp":[{"Feedback_Id":"22","Customer_Id":"543","Feedback_Typ... Commented Jan 21, 2013 at 11:41
  • its a plugin which i got after googling it. but Retval is disturbing a lot Commented Jan 21, 2013 at 11:43
  • That sounds like the URL is not returning JSONP but JSON. You are telling jQuery to expect JSONP though. If the server does not support JSONP or CORS, you cannot access it via Ajax. Please refer to their developer/API documentation. Or is http://musi.php/ a Phonegap thing? It looks strange for a URL. If that's the case, see how you set up JSONP wit Phonegap. Commented Jan 21, 2013 at 11:49
  • @felix: musi.php i reterive data from it as its a api; and m using it for an app in phone gap Commented Jan 21, 2013 at 11:50

3 Answers 3

1

You can use jQuery.parseJSON

Live Demo

jsonobj = $.parseJSON('{"RetVal":"Ok","ValueRsp":[{"Feedback_Id":"22","Customer_Id":"543","Feedback_Type_Id":"1","First_Name":"Tester Tester","Last_Name":"NA","Nick_Name":"Xgcfyxfu"}]}');
alert(jsonobj.RetVal);
alert("jsonobj.ValueRsp[0].Customer_Id: " + jsonobj.ValueRsp[0].Customer_Id);
Sign up to request clarification or add additional context in comments.

12 Comments

i have that format in a url; and i m nt able to use it to display in my html code
jQuery will automatically parse it.
jsonobj you have used this ; but i need to retrive values from a url . wat fr that?
@FelixKling: will you please elaborate or kindly help me in doing so
@rOcKiNgRhO: There is not much we can do. See whether the server supports JSONP (read their API documentation) and what exactly you have to do for it to make it work. If they don't support JSONP or CORS, you'd have to use your server as a proxy. If you have control over the other server though, you can set it up to support JSONP. There are many questions about making Ajax requests to external domains, search for them.
|
0

a little bit of googling would have done it...

var obj = JSON.parse(yourString);
// now use obj.RetVal

Comments

0

you can use the function:

var json_obj = JSON.parse("correct_json_format_string");

now you can use the json_object ot access all the properties:

var return_value = json_obj.RetVal;

Hope that helps.

Comments

Your Answer

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.