2

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; }
} 
Chris W
  • 1,792
  • 15
  • 32
  • 1
    Please note that these are not illegal JSON names. They may not be easy to handle but they are within the specification. – Lasse V. Karlsen Jul 02 '15 at 13:36
  • Hi Lasse, You're correct. I should have clarified that I meant characters which would be illegal in c# class member names. – Chris W Jul 02 '15 at 13:38

1 Answers1

1

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
mythz
  • 141,670
  • 29
  • 246
  • 390