Json object in propertys

(T Roustan) #1

Hello,
I want to put a json object into a property and retrive it.
Ex: i have a node with a property test:'[{"id":"string",tilte:"string"},{"id":"string",tilte:"string"}]'
i want to deserialize this property ton an object with de neojclient driver but this property is always null.

Any ideas ?

thanks
thierry

(Mike French) #2
using (var session = Global.Neo4jDriver.Session())
 {
   // Quick and dirty example, don't recommend ever using session.Run.First.
   var record = session.Run("MATCH (t:TestJson) RETURN t.test").First();
   var type = record[0].GetType();
   var value = record[0];
}

Give's you the result of:
?value
"[{\"id\":\"string\",tilte:\"string\"},{\"id\":\"string\",tilte:\"string\"}]"
?type
{Name = "String" FullName = "System.String"}

So there's a few ways to deserialize that.
ref: JSON Serialization and Deserialization in C#

I went with the non JSON.Net one because I didn't need to mess with nuget.

using (var session = Global.Neo4jDriver.Session())
{
  var record = session.Run("MATCH (t:TestJson) RETURN t.test").First();
  var type = record[0].GetType();
  string value = record[0].As<string>();
  var js = new System.Web.Script.Serialization.JavaScriptSerializer();
  var testObjs = js.Deserialize<dynamic>(value);
}