简体   繁体   中英

ServiceStack.Text and DeserializeFromString where Json names are illegal

I've been using ServiceStack.Text DeserializeFromString for a long time, but in a new project I've hit an issue.

The JSON I need to parse to objects has the following format:

{"http://SomeUrl.com/":{"http://otherUrl.org/schema#name":[{"value":"val1","type":"val2"}]}}

So the object name is prefixed with a URL, so I can't match it to a class member name. I've tried using DataContract to map the names, but it just returns null objects.

Is there another way of doing this using ServiceStack.Text, or do I need to parse the JSON manually?

Any help would be appreciated.

Edit:

With a bit of playing about, I managed to solve this issue using the DataContract attributes. It was failing previously because the classes I specified weren't prefixed correctly. I managed to solve it like so:

[DataContract]
public class Schools
{
    [DataMember(Name = "http://demo.talisaspire.com/")]
    public Items Items { get; set; }
}

[DataContract]
public class Items
{

    [DataMember(Name = "http://purl.org/vocab/aiiso/schema#code")]
    public IEnumerable<Element> Code { get; set; }

    [DataMember(Name = "http://purl.org/vocab/aiiso/schema#knowledgeGrouping")]
    public IEnumerable<Element> KnowledgeGrouping { get; set; }

    [DataMember(Name = "http://purl.org/vocab/aiiso/schema#name")]
    public IEnumerable<Element> Name { get; set; }

    [DataMember(Name = "http://purl.org/vocab/aiiso/schema#organizationalUnit")]
    public IEnumerable<Element> OrganizationalUnit { get; set; }

    [DataMember(Name = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type")]
    public IEnumerable<Element> Type { get; set; }

}

public class Element
{
    public string Type { get; set; }
    public string Value { get; set; }
} 

It would be easier to parse it manually which you can do with:

var obj = JsonObject.Parse(json)
    .Object("http://SomeUrl.com/");

var items = obj.ArrayObjects("http://otherUrl.org/schema#name")[0];

var value = items["value"]; //= val1
var type = items["type"];   //= val2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM