将来自Web服务的字符串转换为JSON

I have a connection to remote DB and I have get some statistical data from the DB. So I have decided to create a web service and get the desired data through it. DB server is SQL Server on Windows and I need to display data in my PHP web page.

First I have written the web service which returns string like

"[
  { 
    "name" : "John", 
    "Age":"54"
  }, 
  {
    "name":"Jack",
    "Age":"33"
  }
 ]"

I have got that string using PHP's file_get_contents() method.

$json = file_get_contents('http://myurl.org/services.asmx/namelistJson');

But there are some unwanted tags return, in the beginning

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://myurl.org/services.asmx?op=namelistJson">

in at the end

</string>

I clean them using

$json = strip_tags(file_get_contents('http://myurl.org/services.asmx/namelistJson'));

Now I have a json string.

I want to use this string as JSON for javascript in order to create a table.

<?php

$json = strip_tags(file_get_contents('http://myurl.org/services.asmx/namelistJson'));

?>
<script>

    var response = $.parse(<?php echo $json; ?>);

    $.each(response, function(i, item) {
        $('<tr>')
                .html("<td>" + response[i].name + "</td>"
                        + "<td>" + response[i].Age + "</td>"
                        )
                .appendTo('#nameList');
    });

</script>

?>
<table id="nameList" class="table">
</table>

I am getting error about missing ] symbol. FireBug says that

SyntaxError: missing ] after element list

How can I convert string to json properly in order to use it on javascript?

EDIT:

If I modify the line

var response = $.parse(<?php echo $json; ?>);

to

var response = <?php echo $json; ?>;

I get ] error still.

HTML output is as the following:

<script>

...

var response = [{"name" : "John",  "Age" : "14"},  {"name" : "40"}, ... ];

...

</script>

No need to create a string and then again convert it to json object, you can directly send json object from webservice and directly use it in the scripts(jquery) as below:

Below is the code in my webservice

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public static object GetGridData() 
{
    DataTable dt = GetRecords(); // Fetch the data from sql in form of a datatable
    var returnData = from row in dt.AsEnumerable()
                     select new
                     {
                        Id = row["Id"]  // Here the Id in row["Id"] specifies the column in database
                         name = row["name"],
                         age = row["age"]
                     };
    return returnData;
}

To access it as object in jquery, use the below code:

 $.ajax({
                url: 'url of webservice along with method', 
                dataType: 'json',
                contentType: 'application/json; charset=utf-8;',
                tyep: 'GET',
                success: function (datas) {
                    var data = datas.d;
                        $.each(data, function (index, value) {
                        $('<tr>').html("<td>" + value.name + "</td>"+ "<td>" + value.Age + "</td>"
                    ).appendTo('#nameList');
                        });
                },
                error: function (error) {
                    alert('error occured');
                }
            });

But if you really want to send it using string and parsing it into json then you can use

var jsonObject = JSON.parse(jsonstring);

or else you can verify if your json string is valid or not by just pasting your json string in jsonlint.com

Hope this helps :)