示例#1
0
 function _normalizedFirst($o, $expr)
 {
     if ($expr == "") {
         return false;
     } else {
         if (preg_match("/^\$(\\[([0-9*]+|'[-a-zA-Z0-9_ ]+')\\])*\$/", $expr)) {
             print "normalized: " . $expr;
             return $expr;
         } else {
             $res = jsonPath($o, $expr, array("resultType" => "PATH"));
             return $res ? $res[0] : $res;
         }
     }
 }
示例#2
0
 protected function replaceJsonPath($matches)
 {
     $value = jsonPath($this->json, $matches[1]);
     return is_array($value) && count($value) == 1 ? reset($value) : $value;
 }
示例#3
0
function json2xml($file)
{
    //Gabriele Cigna
    //header("Content-type:application/xml");
    require_once 'json.php';
    //JSON parser
    require_once 'jpath.php';
    // JSONPath evaluator
    $i = 0;
    $w = 0;
    $doc = new DomDocument();
    $filejson = file_get_contents($file);
    $root = $doc->appendChild($doc->createElement('locations'));
    $jsondecoded = json_decode($filejson);
    $dumbo = new RecursiveArrayIterator($jsondecoded);
    foreach ($dumbo as $key) {
        //trovo gli Id
        if ($key != NULL) {
            foreach ($key as $key => $value) {
                $id[$i] = $key;
                $i++;
            }
        }
    }
    $parser = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
    $o = $parser->decode($filejson);
    $bum = jsonPath($o, "\$..category");
    $k = 0;
    if ($bum != NULL) {
        foreach ($bum as $v) {
            foreach ($v as $s) {
                $category[$k] = $s;
                $k++;
            }
        }
    }
    $location = jsonPath($o, "\$.locations");
    foreach ($location as $key) {
        //trovo i campi
        if ($key != NULL) {
            foreach ($key as $keys => $vall) {
                foreach ($vall as $ciaa => $rrr) {
                    if ($aaa[0] == $ciaa and $w > 0) {
                        break;
                    }
                    $aaa[$w] = $ciaa;
                    $w++;
                }
            }
        }
    }
    $x = 0;
    $metadata = jsonPath($o, "\$.metadata");
    if ($metadata != NULL) {
        foreach ($metadata as $key => $value) {
            foreach ($value as $v) {
                $meta[$x] = $v;
                $x++;
            }
        }
    }
    $metadata = $root->appendChild($doc->createElement('metadata'));
    if ($metadata != NULL) {
        for ($y = 0; $id[$y] == 'creator'; $y++) {
            $k = $y;
        }
        $z = 0;
        for ($k; $k <= sizeof($id) - 1; $k++) {
            $creator = $metadata->appendChild($doc->createElement($id[$k]));
            $creator->appendChild($doc->createTextNode($meta[$z]));
            $z++;
        }
        for ($cont = 0; $cont < $w; $cont++) {
            $campo[] = jsonPath($o, '$..' . $aaa[$cont]);
        }
    }
    $lat = jsonPath($o, "\$..lat");
    $long = jsonPath($o, "\$..long");
    if ($value != NULL) {
        for ($j = 0; $id[$j] != "creator"; $j++) {
            $loc = $root->appendChild($doc->createElement('location'));
            $loc->setAttribute('id', $id[$j]);
            $loc->setAttribute('lat', $lat[$j]);
            $loc->setAttribute('long', $long[$j]);
            $categoria = $loc->appendChild($doc->createElement('category'));
            $categoria->appendChild($doc->createTextNode($category[$j]));
            for ($g = 0; $g < $w; $g++) {
                if (!is_array($campo[$g][$j])) {
                    $tag = $loc->appendChild($doc->createElement($aaa[$g]));
                    $tag->appendChild($doc->createTextNode($campo[$g][$j]));
                }
            }
        }
    }
    return $doc->saveXML();
}
  },
  { "o": { a: { a:2, b:3 },
           b: { a:4, b:5 },
           c: { a: { a:6, b:7}, c:8}
         },
    "p": [ "$..a"
         ]
  }
]
';
$parser = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$tests = $parser->decode($json);
$out = "";
for ($i = 0; $i < count($tests); $i++) {
    for ($j = 0; $j < count($tests[$i]['p']); $j++) {
        $pre = ">";
        if (($pathes = jsonPath($tests[$i]['o'], $tests[$i]['p'][$j], array('resultType' => "PATH"))) && ($values = jsonPath($tests[$i]['o'], $tests[$i]['p'][$j]))) {
            for ($k = 0; $k < count($pathes); $k++) {
                $out .= $pre . " " . $pathes[$k] . " = " . $parser->encode($values[$k]) . "\n";
                $pre = " ";
            }
        }
    }
    $out .= "<hr/>";
}
print $out;
?>
</pre>
</body>
</html>