简体   繁体   English

将json解码为html

[英]decode json as html

So I'm building a web application and I have an ajax request that pings a database (or database cache) and echos back a big thing of json. 因此,我正在构建一个Web应用程序,并且我有一个Ajax请求来ping数据库(或数据库缓存)并回显json的大事。 I'm totally new to json, and when the php pulls from the database I echo json_encode($databaseResults), then it shows up in my html page as a long string. 我对json完全陌生,当php从数据库中提取时,我回显json_encode($ databaseResults),然后它在我的html页面中显示为长字符串。 My question is, how do I convert it and pull out the pieces I need into a nice format? 我的问题是,如何将其转换为所需格式并将其提取出来?

Thanks! 谢谢!

The Json result that was in the page looks like: 页面中的Json结果如下所示:

"[{\\"currentcall\\":\\"1\\",\\"timecalled\\":\\"15:30\\",\\"etaTime\\":\\"15:35\\",\\"departmentID\\":\\"1\\",\\"memberID\\":\\"1\\",\\"callinnum\\":\\"1\\",\\"location\\":\\"Fire House\\",\\"billed\\":\\"N\\",\\"date\\":\\"2\\\\/12\\\\/11\\",\\"firstName\\":\\"Phil\\",\\"lastName\\":\\"asdf\\",\\"email\\":\\"pasdf@gmail.com\\",\\"homephone\\":\\"+19111111111\\",\\"cellphone\\":\\"+11234567891\\",\\"cellphone2\\":null,\\"workphone\\":null,\\"phonenumber5\\":null,\\"phonenumber6\\":null,\\"streetAddress\\":\\"10 asdfnt Dr\\",\\"city\\":\\"\\",\\"username\\":\\"pgsdfg\\",\\"password\\":\\"0623ab6b6b7dsasd3834799fbf2a08529d\\",\\"admin\\":\\"Y\\",\\"qualifications\\":\\"Interior\\",\\"rank\\":null,\\"cpr\\":null,\\"emt\\":null,\\"training\\":null,\\"datejoined\\":null,\\"dateactive\\":null,\\"state\\":\\"DE\\",\\"zip\\":\\"51264\\",\\"pending\\":\\"NO\\",\\"defaultETA\\":\\"7\\",\\"apparatus\\":\\"asdKE-286\\"}]" “[{\\” currentcall \\ “:\\” 1 \\” \\ “timecalled \\”:\\ “15:30 \\”,\\ “etaTime \\”:\\ “15时35 \\”,\\ “DepartmentID的\\”:\\ “ 1 \\”,\\“ memberID \\”:\\“ 1 \\”,\\“ callinnum \\”:\\“ 1 \\”,\\“位置\\”:\\“ Fire House \\”,\\“开票\\”:\\“ ñ\\ “\\ ”日期\\“:\\ ”2 \\\\ / 12 \\\\ / 11 \\“,\\ ”名字\\“:\\ ”菲尔\\“,\\ ”姓氏\\“:\\ ”ASDF \\“,\\”电子邮件\\ “:\\” pasdf@gmail.com \\ “\\ ”HOMEPHONE \\“:\\ ”+ 19111111111 \\“,\\ ”手机\\“:\\ ”+ 11234567891 \\“,\\ ”cellphone2 \\“:空,\\” workphone \\“:null,\\” phonenumber5 \\“:null,\\” phonenumber6 \\“:null,\\” streetAddress \\“:\\” 10 asdfnt Dr \\“,\\” city \\“:\\” \\“,\\”用户名\\ “:\\” pgsdfg \\”,\\ “密码\\”:\\ “0623ab6b6b7dsasd3834799fbf2a08529d \\”,\\ “管理员\\”:\\ “Y \\”,\\ “资格\\”:\\ “内部\\”,\\ “等级\\” :空,\\ “CPR \\”:空,\\ “EMT \\”:空,\\ “培训\\”:空,\\ “datejoined \\”:空,\\ “dateactive \\”:空,\\ “状态\\”:\\ “DE \\”,\\ “拉链\\”:\\ “51264 \\”,\\ “未决\\”:\\ “否\\”,\\ “defaultETA \\”:\\ “7 \\” \\ “设备\\”:\\“asdKE -286 \\ “}]”

There can be multiple results... this is only one result 可能有多个结果...这只是一个结果

EDIT: 编辑:

Basically, I'm trying to pass a bunch of rows in an array into an html file, and take out only the data I need and format it. 基本上,我试图将数组中的一堆行传递到html文件中,并仅取出所需的数据并对其进行格式化。 I don't know if json is the best way to do this or not, just one solution I came up with. 我不知道json是否是执行此操作的最佳方法,只是我想到的一种解决方案。 So if anyone has a better solution that would be awesome. 因此,如果有人有更好的解决方案,那就太好了。

Edit2: This is the jquery I have that makes the request, the php just has echo json_encode ($DBResults); Edit2:这是我发出请求的jQuery,PHP仅具有echo json_encode($ DBResults);

function getResponder(){
    var responders = $.ajax({
            type : "POST",
            url: "/index.php/callresponse/get_responders",
            success: function(html){
                $("#ajaxDiv").html(html);   
            }
    });

setTimeout("getResponder()", 10000);
}

As you flagged this as jquery I assume that you're using jQuery. 当您将其标记为jquery时,我假设您正在使用jQuery。 If you're only going to get the one string you can skip the json part and use jQuery .load() like this $('#result').load('ajax/test.php'); 如果只想获得一个字符串,则可以跳过json部分,并使用jQuery .load(),像这样$('#result').load('ajax/test.php'); that will load the contents from ajax/test.php into #result 会将ajax / test.php中的内容加载到#result中

However if you want to use json you can take a look over at getJSON on the jQuery documentation . 但是,如果要使用json,可以查看jQuery文档上的getJSON You can also use the jQuery parseJSON function which will return the json an javascript object containing the jsonData. 您还可以使用jQuery parseJSON函数 ,该函数将为json返回一个包含jsonData的javascript对象。

Here's an example how you can use parseJSON 这是一个如何使用parseJSON的示例

var object = $.praseJSON(jsonString); //jsonString is the string containing your actual json data
alert(object.location) //Will alert "Fire House" with the given json string

Here's an example of how you can use getJSON in the same way 这是如何以相同方式使用getJSON的示例

$.getJSON('ajax/test.php', function(object) {
  alert(object.location); //Will alert "Fire House" with the given json string
});

If you want to pass parameters as well you can do it like this 如果您也想传递参数,可以这样

$.getJSON('ajax/test.php', 
    { 
        Param1 : "Value1", 
        Param2 : "value2" 
    },
    function(object) {
        alert(object.location);  //Will alert "Fire House" with the given json string
    }
);

If you are trying to send json from javascript to php you can use 如果您尝试将json从javascript发送到php,则可以使用

$jsonArray = jsonDecode($_GET['key']); 

Of course if you're using post you'll write $_POST instead. 当然,如果您使用的是post,则应改写$ _POST。

You have to parse the data into a JSON object, then you can use properties of the object as you wish. 您必须将数据解析为JSON对象,然后可以根据需要使用对象的属性。

Without seeing the specifics, I can tell you that you'll need to use the JSON object to parse the text. 在不了解细节的情况下,我可以告诉您,您将需要使用JSON对象来解析文本。 See more here: http://www.json.org 在此处查看更多信息: http : //www.json.org

var obj = JSON.parse(ajaxResponseText);

You should use php function json_decode which will give you an object or array with all the properties. 您应该使用php函数json_decode ,它将为您提供具有所有属性的对象或数组。

Then you should iterate recursively through this object and add the content of the properties to a string, which should be your final HTML. 然后,您应该递归地遍历此对象,并将属性的内容添加到字符串中,该字符串应该是最终的HTML。

Normally to display JSON response in an html element after jquery( for example ) by web request, try this: 通常,要通过Web请求在jQuery( 例如 )之后的html元素中显示JSON响应 ,请尝试以下操作:

$("#box-content-selector").append(
    "<pre>"+
    JSON.stringify(JSON.parse(data), null, 4)+
    "</pre>"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM