Json DeserializeObject与资源对象中的空格

时间:2015-03-20 08:17:54

标签: c# json

JSON字符串在资源对象中包含一个空格,因此' id'已填写,但“使用ID' ' userstatus' '最后一次活动'是空的。

我在班上尝试了3种不同的方法,但没有一种方法可以工作()

  • 下划线
  • 使用空格
  • 没有空格

我的代码(c#):

这是JSON字符串

{
"resultsList":[  
    {  
      "id":"1",
      "last event":"Mar 20 07:08 AM",
      "use id":"142AD",
      "user status":"offline"
    },
    {  
      "id":"2",
      "last event":"Mar 19 08:07 AM",
      "use id":"1426BD",
      "user status":"offline"
    }
  ]
}

我在中反序列化JSON 结果.cs(表格)

string url = "API url"; var json = new WebClient().DownloadString(url); var results = JsonConvert.DeserializeObject<Rootobject>(json);

Sessions.cs(类)

public class Rootobject
{
    public Resultslist[] resultsList { get; set; }
}

public class Resultslist
{
    public int id { get; set; }
    public string last event { get; set; }
    public string use_id { get; set; }
    public string userstatus { get; set; }

}

3 个答案:

答案 0 :(得分:0)

您案例中最简单的方法是:

string json = "{\"resultsList\":[{\"id\":\"1\",\"last event\":\"Mar 20 07:08 AM\",\"use id\":\"142AD\",\"user status\":\"offline\"},{\"id\":\"2\",\"last event\":\"Mar 19 08:07 AM\",\"use id\":\"1426BD\",\"user status\":\"offline\"}]}";

dynamic o = JsonConvert.DeserializeObject(json);
var result = o["resultsList"][0]["last event"];

答案 1 :(得分:0)

试试这个:

using System;
using Newtonsoft.Json;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var json = @"{""resultsList"":[{ ""id"":""1"",""last_event"":""Mar 20 07:08 AM"",""use_id"":""142AD"",""user_status"":""offline""}, { ""id"":""2"",
      ""last_event"":""Mar 19 08:07 AM"",
      ""use_id"":""1426BD"",
      ""user_status"":""offline""
    }
  ]
}";
            var results = JsonConvert.DeserializeObject<Rootobject>(json);
            for (int i = 0; i < results.resultsList.Length; i++)
            {
                Console.WriteLine("Id:" + results.resultsList[i].id);
                Console.WriteLine("Last Event:" + results.resultsList[i].last_event);
                Console.WriteLine("User ID:" + results.resultsList[i].use_id);
                Console.WriteLine("User Status:" + results.resultsList[i].user_status);
            }
            Console.ReadKey();
        }
    }
    public class Rootobject
    {
        public Resultslist[] resultsList { get; set; }
    }

    public class Resultslist
    {
        public int id { get; set; }
        public string last_event { get; set; }
        public string use_id { get; set; }
        public string user_status { get; set; }

    }
}

答案 2 :(得分:0)

Sessions.cs(类)

我添加了一个JsonProperty,它可以工作。

public class Rootobject
{
    public Resultslist[] resultsList { get; set; }
}

public class Resultslist
{
    public int id { get; set; }
    [JsonProperty("last event")]
    public string lastevent { get; set; }
    [JsonProperty("use id")]
    public string useid { get; set; }
    [JsonProperty("user status")]
    public string userstatus { get; set; }

}