0

Can someone help me to alert below mentioned getJSON value?

Script:

//get char values  
$.getJSON(base_url + "index.php/admin/admin_controller/getjson_stock_data", function (result) {
    console.log(result);
    if(result.auth==="apparel"){
       alert(result.val);
    }
});

JSON response:

{auth: "apparel", val: Array(1)}
auth
:
"apparel"
val
:
Array(1)
0
:
{COUNT(ITEM_CAT): "3"}
length
:
1
__proto__
:
Array(0)
__proto__
:
Object

When I do the alert the results comes as [object Object].Please help me to alert the value 3.

2
  • result.val is an Entire Array. result.val[2] is the third Array element. I would avoid using alert(). Commented Dec 22, 2017 at 10:06
  • This is why you shouldn't use alert() for debugging; it coerces types. USe console.log() instead Commented Dec 22, 2017 at 10:07

3 Answers 3

2
alert(JSON.stringify(result.val[0]["COUNT(ITEM_CAT)"]))
Sign up to request clarification or add additional context in comments.

4 Comments

your code gives me the result [{"COUNT(ITEM_CAT)":"3"}]. But I only need value 3. @zabusa
@EshanGurusinghe modified answer .
thank you very much @zabusa, But still a little question there console.log results give me "3". However I need 3 only without double quotes.
@EshanGurusinghe for debugging never use alert.use console.log().its the proper way.or use debugger;
1

use JSON.stringify function.

$.getJSON(base_url + "index.php/admin/admin_controller/getjson_stock_data", function (result) {
                    console.log(result);
                    if(result.auth==="apparel"){
                       alert(JSON.stringify(result.val));
                       //alert(result.val);
                    }
                });  

Comments

0

You try like this

 var responseAjax = JSON.parse(result);
 alert(responseAjax['val'].COUNT(ITEM_CAT));`

or

$.each(responseAjax['val'],function(i,v){
alert(v.COUNT(ITEM_CAT));    ` 
});`

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.