1

So basically im making a call to an MVC controller and returning the results of a query in JSON format.

<script>
    $("#APINumber").focusout(function () {
        $.getJSON('@Url.Action("CheckAPI", "WellInfo")', { APINumber: $(this).val() }, function (data) {
            if (!data.isDuplicate) {
                toastr.error('API Number does not exist in the database. Please check your API Number.', 'API Number Error')
                $("#APINumber").val("")
            }
            var id = JSON.stringify(data.list);

            $.each(data.list, function (value) {
                viewModel.LeaseName(JSON.stringify(data.list[value]));
            });

            
        });

    });

What I want to do is pull out the value called "LeaseName" from my query. I made the $.each loop to try to parse them, but my textbox I am filling with viewModel.LeaseName(JSON.stringify(data.list[value])); displays the whole JSON string! See below, this is what I get.

{"WellKey":221622,"OperatorKey":21,"LeaseKey":519,"APINumber":"03048374","FormattedAPINumber":"030-48374","RedrillCancelFlag":"  ","WellDesignation":"518G1-34","FieldCode":"052","FieldAreaCode":"05200","FieldName":"Belridge, South","AreaCode":"00","AreaName":"Any Area","DistrictNumber":"4","CACountyCode":"030","CountyName":"Kern","LeaseName":"","WellNumber":"518G1-34","IsActive":1,"WellTypeCodeList":"","WellStatusCode":"A","WellStatusDate":"/Date(1342814313397)/","BLMInterestCode":"N","OperatorName":"Aera Energy LLC","OperatorCode":"A0610","IsCurrentActive":true,"Section":34,"Township":"28S","Range":"21E","BaseMeridian":"MD","IsBLMInterest":false,"IsEPAWell":false,"BaseOfFreshWater":"","AssignedEngineerID":-1,"IsHydraulicallyFractured":true,"AbandonedStandardStatusCode":"Not Abandoned","DirectionalStatusCode":"Directionally drilled","LocationDescription":"Fr SW cor 582 N 405 E","Elevation":"592 KB","IsDryHole":false,"Version":4,"Well2Version":3}

I dont want the whole this in my textbox, I just want to display a SINGLE value from that JSON data.

Any help would be awesome!!!!

EDIT

Tried using this

            $.each(data.list, function (value) {
                var json = JSON.stringify(data.list[value])
                console.log(json["WellKey"]);
            });

I get undefined in the console...

EDIT 2

Solved! No need for loops, I guess using $.getJSON already stringifies it, all I need to do to access my data was using something like data.list[0].FieldName

Thanks Everyone!

1
  • Is that not the stringification of the object located at data.list[value]? Commented Feb 5, 2014 at 20:37

2 Answers 2

1

You simply need:

json["LeaseName"]

See jsFiddle:

http://jsfiddle.net/5BhWr/

However, please be aware that LeaseName has no value in your returned json.

Update

Looking at your code I believe that this will output your value:

data["LeaseName"]

As you are within a getJson call, the data variable will already be json.

If you take a look at this fiddle, it outputs a value in the same manner only WellKey as that has a value:

http://jsfiddle.net/5BhWr/2/

Sign up to request clarification or add additional context in comments.

12 Comments

Thats a great approach, however for some reason im getting back "undefined" in my console window.
Have you seen the fiddle? Give me a min, I'll take a look.
Run this fiddle and you will see it output the WellKey in a div from the json..jsfiddle.net/5BhWr/1
I think from your code that data["LeaseName"] should do it for you.
Your JSFiddle works great, thats exactly what I want! I keep getting 'undefined in the log...even if I just use data.list["WellKey] or any other way....check my post for my edited foreach loop. Maybe im doing it wrong
|
1

From what you write it seems that you have a stringify too many. Assuming that

{"WellKey":221622,"OperatorKey":21,"LeaseKey":519,...}

is the value of

data.list[value]

then simply do

viewModel.LeaseName(data.list[value].LeaseName);

2 Comments

The weird thing is that if I try to do data.list[value].LeaseName...the .LeaseName part doesnt even show up in the list of options to choose from.
Using a combo of your answer and hutchoids, I came up with console.log(data.list[0].FieldName). No needs for loops or strigifying. Thanks!

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.