0

The string I want to parse:

[
    {
    id: "new01"
    name: "abc news"
    icon: ""
    channels: [
    {
    id: 1001
    name: "News"
    url: "http://example.com/index.rss"
    sortKey: "A"
    sourceId: "1"
    },
    {
    id: 1002
    name: "abc"
    url: "http://example.com/android.rss"
    sortKey: "A"
    sourceId: "2"
    } ]
    },
{
    id: "new02"
    name: "abc news2"
    icon: ""
    channels: [
    {
    id: 1001
    name: "News"
    url: "http://example.com/index.rss"
    sortKey: "A"
    sourceId: "1"
    },
    {
    id: 1002
    name: "abc"
    url: "http://example.com/android.rss"
    sortKey: "A"
    sourceId: "2"
    } ]
    }
]
2
  • I had try, read all document but unsuccessfull:D Commented May 9, 2011 at 4:40
  • To an object you created or to a JsonObject? Commented May 9, 2011 at 5:53

2 Answers 2

5

Your JSON isn't actually JSON - you need commas after the fields:

[
    {
    id: "new01",
    name: "abc news",
    icon: "",
    channels: [
    {
    id: 1001,
       ....

Assuming you've done that and that you are using JSON.NET, then you'll need classes to represent each of the elements - the main elements in the main array, and the child "Channel" elements.

Something like:

    public class Channel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string SortKey { get; set; }
        public string SourceId { get; set; }            
    }

    public class MainItem
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Icon { get; set; }
        public List<Channel> Channels { get; set; }
    }

Because there is a mismatch between the C# member naming conventions and the JSON names, you'll need to decorate each member with a mapping to tell the JSON parser what the json fields are called:

    public class Channel
    {
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("sortkey")]
        public string SortKey { get; set; }
        [JsonProperty("sourceid")]
        public string SourceId { get; set; }            
    }

    public class MainItem
    {
        [JsonProperty("id")]
        public string Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("icon")]
        public string Icon { get; set; }
        [JsonProperty("channels")]
        public List<Channel> Channels { get; set; }
    }

Once you've done this, you can parse a string containing your JSON like this:

var result = JsonConvert.DeserializeObject<List<MainItem>>(inputString);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will try your way and notify result. Thanks Damian.
0

yup, it's JsonConvert.DeserializeObject(json string)

try using JsonConvert.SerializeObject(object) to create the JSON.

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.