qqruza
qqruza

Reputation: 1417

Getting a value from JSON string

I am trying to get ID from my JSON but it seems like I am doing something stupid.

Please look at my json and jquery code below.

myData = 'result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"1"},"message":"","result":[{"ID":"1","user_registered":"13th February 2015","display_name":"SMPI","description":"","custom_fields":{"job_title":"Developer","company_name":"SMIO","telephone_number":"+1 343 5325 6456","thumbnail_id":"domain.com\/wp-content\/uploads\/ava.jpg"}}]});';

var myData = JSON.parse(myData);

$(document).ready(function () {
$.each(myData, function () {
    $('<li>' + this.result.ID + '</li>').appendTo('#groups');
});
});

If anyone could help me with my issue I would really appreciate it. Please feel free to amend my jsfiddle:

http://jsfiddle.net/pae5f3j7/

Upvotes: 0

Views: 46

Answers (3)

guest271314
guest271314

Reputation: 1

Try

// call `JSON.stringify` with `myData` argument
var myData = JSON.stringify({
    "respond": 1,
    "paging": {
        "stillmore": 0,
        "perpage": 10,
        "callpage": 1,
        "next": 2,
        "previous": 0,
        "pages": 1,
        "result": "1"
    },
    "message": "",
    "result": [{
        "ID": "1",
        "user_registered": "13th February 2015",
        "display_name": "SMIO API",
        "description": "",
        "custom_fields": {
            "job_title": "Developer",
            "company_name": "SMIO",
            "telephone_number": "+1 343 5325 6456",
            "thumbnail_id": "domain.com\/wp-content\/uploads\/ava.jpg"
        }
    }]
});

// call `JSON.parse` with `myData` argument
var res = JSON.parse(myData);

$(document).ready(function () {
    // call `$.each` with `res.result` array argument
    $.each(res.result, function (key, val) {
        console.log(key, val);
        $("<li>" + val.ID + "</li>").appendTo("#groups");
    });
});

jsfiddle http://jsfiddle.net/pae5f3j7/3/


alternatively

function result(data) {
    return data
};

myData = 'result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"1"},"message":"","result":[{"ID":"1","user_registered":"13th February 2015","display_name":"SMIO API","description":"","custom_fields":{"job_title":"Developer","company_name":"SMIO","telephone_number":"+1 343 5325 6456","thumbnail_id":"domain.com\/wp-content\/uploads\/ava.jpg"}}]});';

myData = eval(myData);

$(document).ready(function () {
    $.each(myData.result, function (key, val) {
        console.log(key, val);
        $("<li>" + val.ID + "</li>").appendTo("#groups");
    });
});

jsfiddle http://jsfiddle.net/pae5f3j7/5/


// removed semicolon `;` at close of string
myData = 'result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"1"},"message":"","result":[{"ID":"1","user_registered":"13th February 2015","display_name":"SMIO API","description":"","custom_fields":{"job_title":"Developer","company_name":"SMIO","telephone_number":"+1 343 5325 6456","thumbnail_id":"domain.com\/wp-content\/uploads\/ava.jpg"}}]})'; 


myData = JSON.parse(myData.split(/^.*\(|\)$/)[1]);

$(document).ready(function () {
    $.each(myData.result, function (key, val) {
        console.log(key, val);
        $('<li>' + val.ID + '</li>').appendTo('#groups');
    });
});

jsfiddle http://jsfiddle.net/pae5f3j7/6/

Upvotes: 1

ste-fu
ste-fu

Reputation: 7497

jQuery $.each takes an anonymous function with two variables, usually referred to as idx and elem, referencing the index of the array and the element of the array.

Your . each function should look more like this:

$(myData).each(function (idx, elem) {

    var item = "<li>" + $(elem).result.ID + "</li>";
    $("#groups").append(item);
});

This assumes that myData is a Jquery array.

Upvotes: 1

Cosmin
Cosmin

Reputation: 2214

Your JSON is not in the correct format and you get SyntaxError: Unexpected token r when you're trying to parse it.

var data= 'result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"1"},"message":"","result":[{"ID":"1","user_registered":"13th February 2015","display_name":"SMIO API","description":"","custom_fields":{"job_title":"Developer","company_name":"SMIO","telephone_number":"+1 343 5325 6456","thumbnail_id":"domain.com\/wp-content\/uploads\/ava.jpg"}}]});';


data=data.replace('result(','');
data=data.replace(');','');

var parsedData= JSON.parse(data);
// you can now get ID ( example : parsedData.result[0].ID )

Upvotes: 2

Related Questions