0

I've received data fro Json as below But I don't understand much about Json object and Json array. I've always confused with it. another hand I would like to ask expert here. How can I passing data from this json data to any div after ajax respond

Issue can't passing data to html element from this json

Here is my json data

{
    "res": {
        "nav": [
            {"text":"SERVICES"},
            {"text":"CooSurf"},
            {"text":"CooMartsdf"},
            {"text":"Devices"}, 
            {"text":"asdf"},
            {"text":"Press Release"},
            {"text":"Speach"},
            {"text":"fqas"},
            {"text":"Network Coverages"},
            {"text":"CooSurf"},
            {"text":"CooMartsdf"},
            {"text":"Devices"},
            {"text":"test"}
        ],
        "content": [
            {"name":"Englishasdfasdf"},
            {"name":"kh_nameasdfasdf"}, 
            {"name":"asdfasdfasdf"}, 
            {"name":"sdfasdfasdf"}, 
            {"name":"English"}, 
            {"name":"English"}, 
            {"name":"FQAS"}, 
            {"name":"Network Coverages"}, 
            {"name":"kh_nameasdfasdf"}
        ], 
        "promot": [
            {"title":"Englishasffasdf"}, 
            {"title":"kh_namedfasdfasdf"}, 
            {"title":"English"}, 
            {"title":"English"}, 
            {"title":"English"}, 
            {"title":"asdfasdfas"}, 
            {"title":"asdfasdfasdf"}, 
            {"title":"asdfasdfasd"}
        ] 
    }
}

Here is my ajax

 $.ajax({
                type: method,
                url: url,
                data: $("#searching_from").serialize(),
                dataType: "json",
                cache: false,
                beforeSend: function () {
                    $('#myModal').modal('show');
                    $(".loadings").css({'display': 'inline'});
                }, success: function (data) {
                   var json = data.res;
                    var obj = JSON.parse(json);
                    console.log(obj );
                    $(".modal-body").append(data.res);
                    $(".loadings").css({'display': 'none'});
                }
            });

** This is function in Model **

public function search_data($keyword) {

        if ($this->msearch($keyword, 'text', 'nav') == TRUE) {

            $res['nav'] = $this->gsearch($keyword, 'text', 'text', 'nav');
        }if ($this->msearch($keyword, 'name', 'content') == TRUE) {

            $res['content'] = $this->gsearch($keyword, 'name', 'name', 'content');
        }
        if ($this->msearch($keyword, 'title', 'promot') == TRUE) {

            $res['promot'] = $this->gsearch($keyword, 'title', 'title', 'promot');
        } else {
            $res[] = false;
        }
        return json_encode(array('res' => $res));
    }

    public function msearch($keyword, $like_col, $table) {

        $this->db->like($like_col, $keyword);
        $query = $this->db->get($table);
        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return false;
        }
    }

    public function gsearch($keyword, $like_col, $select, $table) {


        $this->db->select($select);
        $this->db->like($like_col, $keyword);
        $query = $this->db->get($table);
        return $query->result();
    }
1
  • Please post the code for what you've tried so far. Commented Jul 29, 2015 at 17:45

1 Answer 1

1

Let's call your json data, which is an object, data. I assume you want to pass the list of names in your html element. With jQuery do

$(yourElement).html(data.res.content.map(function(object){
    return object.name;
}).join(", "));

Hope this fits your needs.

Updated I removed the use of JSON.parse above because jQuery automatically parse json into a js object.

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

4 Comments

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
Oh I've forgotten that jQuery already make this parsing en give you the JS object. So I update my answer.
Thanks I'm waiting you sire
I get TypeError: data.res is undefined

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.