简体   繁体   中英

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?

I have this 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. 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

Then you'll need to convert the array to json using php json_encode()

Two solutions:

PURE JQUERY

Using jQuery's parseXML , combined with jQuery get you can have the object you need:

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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