使用嵌套的json数据创建rest api

时间:2017-04-22 15:49:43

标签: c# .net json rest

我有一个json对象,我希望通过rest api保存在数据库中:

    {"name":"gg",
    "fragments":
    [
      "{
          \"cadence\":\"NORMAL\",
          \"distance\":1.0,
          \"inTrainingDistance\":0.0,
          \"inTrainingSeconds\":0,
          \"inTrainingState\":\"n\",
          \"minutes\":5,
          \"points\":[],
          \"seconds\":0,
          \"totalSeconds\":300,
          \"unique_id\":0
      }",
      "{
        \"cadence\":\"NORMAL\",
        \"distance\":1.0,
        \"inTrainingDistance\":0.0,
        \"inTrainingSeconds\":0,
        \"inTrainingState\":\"n\",
        \"minutes\":5,
        \"points\":[],
        \"seconds\":0,
        \"totalSeconds\":300,
        \"unique_id\":1
      }"
    ],
 "summary":
    "{
    \"data\":
        {
        \"day\":20,
        \"hours\":20,
        \"minutes\":8,
        \"month\":4,
        \"seconds\":11,
        \"year\":2017
        },
    \"favourite\":false,
    \"name\":\"gg\",
    \"reward\":\"f\",
    \"totalCheckpoints\":2,
    \"totalDist\":2.0,
    \"totalTime\":\"10:00\"
    }"
}

我在api中的模型看起来像这样:

public class PlanModel
    {
        public String name { get; set;}
        public List<fragments> fragments { get; set;}
        public summary summary { get; set; }

    }

    public class fragments
    {
        public String cadence { get; set; }
        public float distance { get; set; }
        public float inTrainingDistance { get; set; }
        public int inTrainingSeconds { get; set; }
        public String inTrainingState { get; set; }
        public int minutes { get; set; }
        public List<points> point { get; set; }
        public int seconds { get; set; }
        public int totalSeconds { get; set; }
        public int unique_id { get; set; }
    }

    public class summary
    {
        public data data { get; set; }
        public bool favourite { get; set; }
        public string name { get; set; }
        public string reward { get; set; }
        public int totalCheckpoints { get; set; }
        public float totalDist { get; set; }
        public TimeSpan totalTime { get; set; }
    }
    public class points
    {
        public float altitude { get; set; }
        public float latitude { get; set; }
        public float longitude { get; set; }
        public int time { get; set; }
    }
    public class data
    {
        public int day { get; set; }
        public int hours { get; set; }
        public int minutes { get; set; }
        public int month { get; set; }
        public int seconds { get; set; }
        public int year { get; set; }
    }

当我尝试通过get函数保存这些数据时出现问题。只有名称保存正确,其余字段保持为空。我正在使用restlet客户端扩展来谷歌浏览器发送请求。 这是我用来获取数据的代码

public HttpResponseMessage Post([FromBody]PlanModel value)
        {
            String name = value.name;
            String some= value.summary.data.minutes.ToString();
            HttpResponseMessage response;
            response = Request.CreateResponse(HttpStatusCode.Created);
            return response;

        }

请求如下:

follow_redirects:true
method:POST
url:localhost:58202/api/Plan/
digest_username:
digest_password:
consumer_key:
consumer_secret:
access_token:
token_secret:
signature_type:query
header_name_1:Content-Type
header_value_1:application/json
body:{"name":"gg",
 "fragments":
    [
      "{
          \"cadence\":\"NORMAL\",
          \"distance\":1.0,
          \"inTrainingDistance\":0.0,
          \"inTrainingSeconds\":0,
          \"inTrainingState\":\"n\",
          \"minutes\":5,
          \"points\":[],
          \"seconds\":0,
          \"totalSeconds\":300,
          \"unique_id\":0
      }",
      "{
        \"cadence\":\"NORMAL\",
        \"distance\":1.0,
        \"inTrainingDistance\":0.0,
        \"inTrainingSeconds\":0,
        \"inTrainingState\":\"n\",
        \"minutes\":5,
        \"points\":[],
        \"seconds\":0,
        \"totalSeconds\":300,
        \"unique_id\":1
      }"
    ],
 "summary":
    "{
    \"data\":
        {
        \"day\":20,
        \"hours\":20,
        \"minutes\":8,
        \"month\":4,
        \"seconds\":11,
        \"year\":2017
        },
    \"favourite\":false,
    \"name\":\"gg\",
    \"reward\":\"f\",
    \"totalCheckpoints\":2,
    \"totalDist\":2.0,
    \"totalTime\":\"10:00\"
    }"
}

0 个答案:

没有答案