How i can add a List to the JSON POST request with webwrite (required for RESTful interface of Neo4j)

5 ビュー (過去 30 日間)
The following RESTful JSON Interface is needed to submit a Query (e.g. Node creation) to a Neo4j-Database:
{
"statements" : [ {
"statement" : "CREATE (n) RETURN id(n)"
} ]
}
The JSON-Object is created internal the WEBWRITE-Function. The nested structure can be created in Matlab and is compiled to JSON by the WEBWRITE-Function as well. I was unable to create a JSON List (Array) by using the following approach (Cell Array):
api = 'http://localhost:7474/';
url = [api 'db/data/transaction/commit'];
statem{1,1} = 1;
ab = struct('statement', 'CREATE (n) RETURN id(n)');
statem{1,1} = ab;
statems = struct('statements', statem);
options = weboptions('ContentType','json', 'MediaType','application/json', 'RequestMethod','POST', 'Username', 'neo4j', 'Password', 'test');
data = webwrite(url, statems, options);
JSON-Result created by the function above:
{
"statements": {
"statement": "CREATE (n) RETURN id(n)"
}
}
Is there a way to create a List in the JSON-Object created by WEBWRITE?

採用された回答

Robert Snoeberger
Robert Snoeberger 2015 年 12 月 15 日
編集済み: Robert Snoeberger 2015 年 12 月 15 日
To create a JSON Array, you should use a cell array. However, you should be careful when passing a cell array as a value input argument to the function struct [1]. There are a few ways to proceed.
You could avoid the struct function when building the "statements" name-value pair:
>> ab = struct('statement', 'CREATE (n) RETURN id(n)');
>> statems.statements = {ab}
statems =
statements: {[1x1 struct]}
>>
You could add an extra pair of curly braces '{}' when the value input argument to struct is a cell array:
>> ab = struct('statement', 'CREATE (n) RETURN id(n)');
>> statems = struct('statements', {{ab}})
statems =
statements: {[1x1 struct]}
>>
Just make sure that the display of the variable statems includes a pair of curly braces '{}' around [1x1 struct]:
statems =
statements: {[1x1 struct]}

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJSON Format についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by