1

I am having trouble in converting some string to JSON.

I use C# WebAPI to Deserialize string

  1. First: List<GetBookInfoModel> list = JsonConvert.DeserializeObject<List<GetBookInfoModel>>(strOutput01);

  2. Second:List <GetBookDetInfoModel> list = JsonConvert.DeserializeObject<List<GetBookDetInfoModel>>(strOutput01);

And my Json String is like:

  1. First: [{"allbook ":{ " count01 " :3}, " late ":{ " count02 ":0}}]

  2. Second: [{"num ":1, " bookname ":"AAAAA","FinTime ":"2017"},{"num ":2, " bookname ":"iOS","FinTime ":"2017"},{"num ":3, " bookname ":"Visual","FinTime ":"2017"}]

And my Model class is like..

public class GetBookInfoModel
{
    List<GetBookSecondInfoModel> allbook { get; set; }
    List<GetBookSecondInfoModel> late { get; set; }
}
public class GetBookSecondInfoModel
{
    public string count01 { get; set; }

    public string count02 { get; set; }
}


public class GetBookDetInfoModel
{
    public string num { get; set; }

    public string bookname { get; set; }

    public string FinTime { get; set; }
}

But now it returns null.

How can I resolve this problem?Thanks.

1
  • 1
    In the JSON strings, there are a lot of spaces in your property names. Try to fix that first. Commented Mar 1, 2017 at 9:21

1 Answer 1

1

It turns out there are 3 issues for the former.

1) The properties of GetBookInfoModel should be public.

public class GetBookInfoModel
{
    public List<GetBookSecondInfoModel> allbook { get; set; }
    public List<GetBookSecondInfoModel> late { get; set; }
}

2) The unnecessary spaces between paired quotes in JSON should be removed.

3) Both allbook and late in JSON should be followed by [].

[{"allbook":[{ "count01"  :3}], "late":[{ "count02":0}]}]

Similar for the latter.

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.