English 中文(简体)
Using JSON with XQuery page
原标题:

So I am using a jQuery plugin (jsonp) to make a cross-domain call to an api and getting JSON data back. I need to somehow get this data into my XQuery page. I m using Marklogic server to store all my XML data and I know it has some XDMP functions to handle JSON data, it is getting the JSON from javascript to XQuery that is giving me a pain.

Any ideas on how to go about this?

最佳回答

I m not 100% sure what is giving you problems but will try to offer up a solution that will hopefully get you on your way.

There is an open source project on GitHub called MLJSON (https://github.com/marklogic/mljson/wiki). It can take a JSON string, parse it and return an XML document that MarkLogic can easily make use of.

If understanding the internal structure of the XML isn t appealing (even though it is fairly straightforward, it is undocumented), the project also includes a path parser to extract bits out of the parsed JSON document. Here s a quick sample XQuery page that will take some JSON sent to the server as a POST or GET parameter, parse it and pull out a value:

xquery version "1.0-ml";

import module namespace json="http://marklogic.com/json" at "lib/json.xqy";
import module namespace path="http://marklogic.com/mljson/path-parser" at "lib/path-parser.xqy";

let $jsonString := xdmp:get-request-field("json")
let $jsonXML := json:parse($jsonString)
let $firstName := path:select($jsonXML, "author.firstName", "json")

return concat("First name: ", $firstName)

If the above script is passed a JSON document like:

{
    "author": {
        "firstName": "Noam",
        "lastName": "Chomsky"
    }
}

It will return the string: "First name: Noam".

MLJSON has a number of other features that I won t go over here, but will mention that it has functions to construct JSON objects, arrays, etc and serialize them out as a JSON string.

Hope that helps.

问题回答

暂无回答




相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签