0

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/

3 Answers 3

2

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 )
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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/

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.