0
votes

I am parsing a string variable into a Json Object with the following code:

string[] rInfo = r.Info.ToString().Split('|'); 
dynamic JSON_Obj = JObject.Parse(rInfo[0]);

And it is looking fine in the Code, meaning if i check the object at runtime it has the right contents. However, after i store it in RavenDB it looks like this:

{"street": {
    "$type": "Newtonsoft.Json.Linq.JValue, Newtonsoft.Json",
    "$values": []
},
"country": {
    "$type": "Newtonsoft.Json.Linq.JValue, Newtonsoft.Json",
    "$values": []
}}

For example in country should be something like "ES" or "GB".

I am storing the JSON Object as part of a document like this:

                PATCHED_Doc Doc = new PATCHED_Doc()
                {
                    Info = JSON_Obj, 
                    Value = "test",
                    Id = r.Id,
                    Date = r.Date,
                };

                session.Store(Doc);
                session.SaveChanges();

Thank you in advance for your help.

1
You need to debug to see what actually JSON.NET parses your string to and why - fahadash
In debugging it looks like this: {"street":"Castelló", "country":"ES", ...}. Same object, once saved, looks in RavenDB like described above... - Martin
@fahadash Forgot to address my comment above to you... - Martin
I think dynamic objects aren't a good idea. Either you map your types properly using JSON.NET attributes. If you can't map them, then try anonymous types. - fahadash

1 Answers

0
votes

fahadash was right. I had to crate a class to map the attributes.

var JSON_Obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Infolist>(strJSON);

Infolist being:

public class Infolist
{
    public string street { get; set; }
    public string coutnry { get; set; }
    ...
}

Like this it is working...