Showing posts with label Json. Show all posts
Showing posts with label Json. Show all posts

Tuesday, April 8, 2014

[PHP][JSON][Example] Print object value of multiple JSON objects using php

$json = '{
    "john":[{"k_id":"1","en_name":"John Chan"}],
    "mary":[{"k_id":"1","en_name":"Mary Li"}],
    "sue":[{"k_id":"1","en_name":"Sue Lam"}]  
}';
$catalog = json_decode($json);

if you want to got john's detail
echo $catalog->john[0]->{'en_name'}.", ";
echo $catalog->{'john'}[0]->{'en_name'};
Result:
John Chan, John Chan

if you want to got john's detail
echo $catalog->mary[0]->{'en_name'}.", ";
echo $catalog->{'mary'}[0]->{'en_name'};

Result:
Mary Li, Mary Li

Dynamic load get key
$name= "john";
echo $catalog->{$name}[0]->{'en_name'};
$name= "sue";
echo $catalog->{$name}[0]->{'en_name'};
******
And now let add a record for John and Sue:
$json = '{
    "john":[{"k_id":"1","en_name":"John Chan"},
            {"k_id":"1","en_name":"Johnny Chan"}],
    "mary":[{"k_id":"1","en_name":"Mary Li"}],
    "sue":[{"k_id":"1","en_name":"Susam Lam"},
           {"k_id":"1","en_name":"Sue Lam"}]  
}';
$catalog = json_decode($json);

//if you want to got ppl's eng name
echo "John's name: ".$catalog->john[0]->{'en_name'}.'<br />';
echo "Sue's name: ".$catalog->sue[1]->{'en_name'};

Result:
John's name: John Chan
Sue's name: Sue Lam

Sunday, April 6, 2014

[jQuery][Json][Solved] Uncaught SyntaxError: Unexpected token o

The json sample:
({
    "status": "ok",
    "type": "img",
    "items": [
        {
            "title": "\u65b9\u6cd5 ",
            "img": "http:\/\/www.abc.com.hk\/086347434_n.jpg",
            "url": "http:\/\/www.abc.com.hk\/DK.php?id=ADkRZBEr"
        },
        {
            "title": "\u6703\u54e1\u535a\u5ba2",
            "img": "http:\/\/www.abc.com.hk\/993.jpg",
            "url": "http:\/\/www.abc.com.hk\/ADsRZBEuA3QMKQ\/"
        }
    ]
})

 my case is using jquery JSON.parse to parse json :

    var url;
    url = "http://www.abc.com.hk/ul?id="+id+"&jsoncallback=?";
    $.getJSON(url, function (json) {
        if (json.status == "ok") {
            result[json.type] = json.status;
            if (currentType == json.type) { swapItemsTo(json.type, true);}
        } else {
        }
        var json = JSON.parse(json);
        console.log(json.type);

    }).error(function(json){
    });
By found the json in my case is already in json format,
so don't need to parse the json,
you can call the json value directly is okay,
It's won't cause the SyntaxError:

    var url;
    url = "http://www.abc.com.hk/ul?id="+id+"&jsoncallback=?";
    $.getJSON(url, function (json) {
        if (json.status == "ok") {
            result[json.type] = json.status;
            if (currentType == json.type) { swapItemsTo(json.type, true);}
        } else {
            alert(json.message);
        }
        console.log(json.type);
    }).error(function(json){
    });

So if you see the error "Uncaught SyntaxError: Unexpected token o ", please check is the value you call is already in json format.

Reference:
http://stackoverflow.com/questions/8081701/i-keep-getting-uncaught-syntaxerror-unexpected-token-o