简体   繁体   English

如何使用PHP和jquery将JSON转换为对象文字

[英]How to turn JSON to object literal using PHP and jquery

Is there a concise neat way to turn an XML feed into an JavaScript object literal? 是否有一种简洁明了的方法将XML提要转换为JavaScript对象文字?

I have this XML feed 我有这个XML feed

<jobs>
    <industry>
        <name>Technology</name>
        <area>Refrigiration</area>
        <area>Information Technology</area>
        <area>Electronics</area>
        <area>Avionics</area>
    </industry>

    <industry>
        <name>Agriculture</name>
        <area>Agri-Tourism</area>
        <area>Animal Husbandry</area>
        <area>Harvesting</area>
        <area>Poultry</area>
    </industry>
</jobs>

and wish to turn it to: 并希望将其转换为:

var jobs = [
    {
        "name"  : "Technology",
        "areas" : [ "Refrigiration" , "Information Technology", "Electronics", "Avionics" ]
    },

    {
        "name"  : "Agriculture",
        "areas" : [ "Agri-Tourism" , "Animal Husbandry", "Harvesting", "Poultry" ]      
    },

    {
        "name"  : "Media",
        "areas" : [ "Journalism" , "Camera person", "Reality tv person", "Commentator" ]        
    }       
];

I succeeded in encoding the JSON object using php. 我成功地编码使用PHP的JSON对象。 What I am missing is the rest. 我所缺少的是其余的。

echo json_encode(simplexml_load_string("<jobs>
    <industry>
        <name>Technology</name>
        <area>Refrigiration</area>
        <area>Information Technology</area>
        <area>Electronics</area>
        <area>Avionics</area>
    </industry>

    <industry>
        <name>Agriculture</name>
        <area>Agri-Tourism</area>
        <area>Animal Husbandry</area>
        <area>Harvesting</area>
        <area>Poultry</area>
    </industry>
</jobs>"));

This gives you: 这给您:

{
    "industry": [
        {
            "name": "Technology",
            "area": [
                "Refrigiration",
                "Information Technology",
                "Electronics",
                "Avionics"
            ]
        },
        {
            "name": "Agriculture",
            "area": [
                "Agri-Tourism",
                "Animal Husbandry",
                "Harvesting",
                "Poultry"
            ]
        }
    ]
}

You need to convert your XML to an array, https://stackoverflow.com/questions/4844476/xml-to-php-array 您需要将XML转换为数组https://stackoverflow.com/questions/4844476/xml-to-php-array

Then you'll need to convert the array to json using php json_encode() 然后,您需要使用php将该数组转换为json json_encode()

Two solutions: 两种解决方案:

PURE JQUERY 纯JQUERY

Using jQuery's parseXML , combined with jQuery get you can have the object you need: 结合使用jQuery的parseXMLjQuery,您可以获得所需的对象:

$.get("http://www.example.com/path/to/file.xml", function(data){
   var xml = $.parseXML(data);
   console.log(xml);
});

PHP+JQUERY PHP + JQUERY

if you've already parsed the object into a json, just print it into an html file and get that file using jquery's getJSON 如果您已经将对象解析为json,只需将其打印到html文件中,然后使用jquery的getJSON获取该文件

$.getJSON("http://www.example.com/json/feed",function(data){
   console.log(data);
});

如果您在JavaScript回调中接收到JSON编码的字符串,则可能需要运行$ .parseJSON()以使jQuery将其视为JSON对象而不是字符串

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

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