簡體   English   中英

無法在C#中使用動態更改的節點反序列化JSON字符串

[英]Unable to de-serialize a JSON string with a dynamically changing node in C#

我的JSON看起來像這樣-

{"0abc34m": {"time": "13 Mar 17, 4:50:02 PM", "pd": "oscar"}}

我正在使用此代碼反序列化-

MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(JSONstring));
ms.Position = 0;
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Rootobject));
Rootobject myDataTypObj = (Rootobject)jsonSerializer.ReadObject(ms);

我使用了Visual Studio函數“將JSON作為類粘貼”函數來生成這些類-

public class Rootobject
{
    public _00A462 _00a462 { get; set; }
}

public class _00A462
{
    public string time { get; set; }
    public string pd { get; set; }
}

我想訪問JSON的“時間”和“ pd”成員。

JSON的第一部分是一個數字,每次接收到新的JSON字符串時都會更改。

我沒有收到任何錯誤,但myDataTypObj的_00A462為空值。

我只關心層次結構中第二級的字段,我正確地解決了這個問題嗎?

一種簡單的方法是使用JSON.Net並將JSON反序列化為Dictionary

using Newtonsoft.Json;

class Program
{
    static void Main( string[] args )
    {
        var jsonString = "{\"0abc34m\":{\"time\":\"13 Mar 17, 4:50:02 PM\",\"pd\":\"oscar\"}}";
        Dictionary<string, Data> data;
        data = JsonConvert.DeserializeObject<Dictionary<string, Data>>( jsonString );
        var key = data.First().Key; // 0abc34m
        var time = data.First().Value.TimeString; // 13 Mar 17, 4:50:02 PM
        var pd = data.First().Value.DataString; // oscar
    }
}

public class Data
{
    [JsonProperty("time")]
    public string TimeString { get; set; }
    [JsonProperty("pd")]
    public string DataString { get; set; }
}

類似於Rufo爵士使用Json.Net來將其序列化為JObject的答案:

DotNetFiddle示例

var json = "{\"0abc34m\": {\"time\": \"13 Mar 17, 4:50:02 PM\", \"pd\": \"oscar\"}}";
dynamic d = JObject.Parse(json);
var values = (d.Properties() as IEnumerable<JProperty>)
  .First()
  .Value as JObject;

var time = DateTime.Parse(((values.Properties() as IEnumerable<JProperty>)
  .First()
  .Value as JValue).Value as string);
var pd = ((values.Properties() as IEnumerable<JProperty>)
  .Skip(1)
  .First()
  .Value as JValue).Value as string;

Console.WriteLine(time);
Console.WriteLine(pd);

結果:

3/13/2017 4:50:02 PM
oscar

如果您知道JSON的確切結構,則可以讀取第一個(隨機)數字,將其替換為具有已知標識符的字符串,然后解析為JSON。

接下來的代碼需要調試!!!

string json_string = ...;
int first_quot_pos = json_string.IndexOf("\"");
int second_quot_pos = json_string.IndexOf("\"", first_quot_pos + 1);
string random_number = json_string.Substring(first_quot_pos, second_quot_pos - first_quot_pos);
string updated_json_string = json_string.Replace(random_number, "number");

您的JSON現在應如下所示:

{"number": {"time": "13 Mar 17, 4:50:02 PM", "pd": "oscar"}}

然后使用updated_json_string

MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(updated_json_string));
ms.Position = 0;
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Rootobject));
Rootobject myDataTypObj = (Rootobject)jsonSerializer.ReadObject(ms);

現在,您的Rootobject應該看起來像這樣:

public class Rootobject
{
    public Number number { get; set; }
}

public class Number
{
    public string time { get; set; }
    public string pd { get; set; }
}

現在,您有了具有myDataTypObj.number.timemyDataTypObj.number.pd對象。 你也有random_number

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM