0

I need to parse the JSON data which is collection of array using C#.

ItemRelations: [
    {
        rel: "System.Links.H-Forward",
        source: {id: 123456,url: "https://somename.domain.com/DefaultCollection/_apis/wit/Items/123456"},
        target: {id: 231856,url: "https://somename.domain.com/DefaultCollection/_apis/wit/Items/231856"}
    }
]

I can parse simple JSON string but when it comes the scenario like above how should i proceed?

5
  • 2
    refer stackoverflow.com/questions/15726197/… link.it might help u Commented Oct 4, 2016 at 9:17
  • Search for Newtonsoft.Json and read some docs. It's really powerful, and you probably dont even need to do much. Also visual studio has a function Edit->Paste Special -> Paste JSON as Classes. Commented Oct 4, 2016 at 9:17
  • 2
    Have a look at newtonsoft.com/json. This might help Commented Oct 4, 2016 at 9:17
  • Instead of a simple object or array, the JSON you pasted is an array of objects. Hope that helps. Commented Oct 4, 2016 at 9:17
  • The posted text is not valid json, specifically the keys need to be embedded in quotes. Commented Oct 4, 2016 at 9:45

1 Answer 1

0

You can generate class for your json using Json2csharp. Then using Json.Net from nuget:

void Main()
{
    var json = @"{ItemRelations: [
    {
        rel: ""System.Links.H-Forward"",
        source: {id: 123456,url: ""https://somename.domain.com/DefaultCollection/_apis/wit/Items/123456""},
        target: {id: 231856,url: ""https://somename.domain.com/DefaultCollection/_apis/wit/Items/231856""}
    }
]}";

    var parsed = JsonConvert.DeserializeObject<RootObject>(json);

    //Linqpad
    //parsed.Dump();
}

public class Source
{
    public int id { get; set; }
    public string url { get; set; }
}

public class Target
{
    public int id { get; set; }
    public string url { get; set; }
}

public class ItemRelation
{
    public string rel { get; set; }
    public Source source { get; set; }
    public Target target { get; set; }
}

public class RootObject
{
    public List<ItemRelation> ItemRelations { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.