0

I'm not sure if I'm just being silly, although I wasn't able to find this anywhere, but I am json_encode()ing some database outputs and outputting them to the page, and reading them using $.parseJSON, but I want to get a specific value from the array. Here is an example of what I'm trying to accomplish.

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

var json = $.parseJSON(j);
$(json).each(function(i, val) {
  $.each(val, function(k, v) {
    console.log(k['uid']) // <-- This is where I want to just output the UID from each array results
});

Now, to put into words what I'd like to accomplish is to just pull the UID (or name or profile_img, just a value in general) from this array, so I can later insert the values into a div.

Any help is much appreciated, thank you!

1
  • Did you try running it? Did you get an error? Where are you stuck? Commented May 31, 2016 at 4:22

6 Answers 6

3

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

var json = $.parseJSON(j);
$(json).each(function(i, val) {
  console.log(val['uid']);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

I would have avoided jQuery for this manipulation.

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';
var parsed = JSON.parse(j);
var uids = parsed.map(function(item) {
  return item.uid;
});
console.log(uids);

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

Comments

0

try this :-

using JS only:

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';
j = JSON.parse(j);
j.map((value)=>{
    console.log(value['uid'])
});

With jQuery:-

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

var json = $.parseJSON(j);
$(json).each(function(i, val) {
  console.log(val['uid']);
});

Comments

0

After parsing your json object to an array list you can just use vanilla map function as below

function stuff(item,index) {
    //Here can do any manipulation with your object
    return item.uid;
}

function myFunction() {
    document.getElementById("myDiv").innerHTML = myArrayList.map(stuff);
}

Comments

0

No 2nd level of each is required.

try this:

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

var json = $.parseJSON(j);
$.each(json, function (i, obj) {
	console.log(obj.uid);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

This may be of help to you.

   var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';

        var json = $.parseJSON(j);
        var l=json.length;
        var arr=[];
        for(var i=0;i<l;i++){
            arr.push(json[i]['uid']);
        }
        console.log(arr);//you can use this array for whatever purpose you intend to

Comments

0

Not necessaray use Jquery!

var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';
var parsedJson = JSON.parse(j);
var uidsArray = parsedJson.map( function(entry){
   return entry.uid;
});

console.log(uidsArray); // output: [1,2]

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.