代码之家  ›  专栏  ›  技术社区  ›  STORM

反序列化JSON对象时出错“分析值时遇到意外字符:<。路径“”,第0行,位置0

  •  1
  • STORM  · 技术社区  · 6 年前

    我正在用这个测试 https://github.com/ysharplanguage/FastJsonParser/blob/master/JsonTest/TestData/fathers.json.txt .

    using Newtonsoft.Json;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using Newtonsoft.Json.Linq;
    using System.Threading.Tasks;
    
    namespace AMServices.Controllers
    {
        public class FathersData
        {
            public Father[] fathers { get; set; }
        }
    
        public class Someone
        {
            public string name { get; set; }
        }
    
        public class Father : Someone
        {
            public int id { get; set; }
            public bool married { get; set; }
            // Lists...
            public List<Son> sons { get; set; }
            // ... or arrays for collections, that's fine:
            public Daughter[] daughters { get; set; }
        }
    
        public class Child : Someone
        {
            public int age { get; set; }
        }
    
        public class Son : Child
        {
        }
    
        public class Daughter : Child
        {
            public string maidenName { get; set; }
        }
    
        public class StreamerController : ApiController
        {
            static readonly JsonSerializer _serializer = new JsonSerializer();
            static readonly HttpClient _client = new HttpClient();
    
            [HttpPost]
            [Route("streamer/stream")]
            public async Task<IHttpActionResult> stream()
            {
                string apiUrl = "https://github.com/ysharplanguage/FastJsonParser/blob/master/JsonTest/TestData/fathers.json.txt";
    
                using (var stream = await _client.GetStreamAsync(apiUrl).ConfigureAwait(false))
                using (var reader = new StreamReader(stream))
                using (var json = new JsonTextReader(reader))
                {
                    if (json == null)
                        StatusCode(HttpStatusCode.InternalServerError);
    
                    JsonSerializer serializer = new JsonSerializer();
    
                    JObject obj = JObject.Load(json);
                    // Father f = serializer.Deserialize<Father>(json);
                }
    
                return StatusCode(HttpStatusCode.OK);
            }
        }
    }
    

    这个代码怎么了?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Kalten    6 年前