我需要从这个JSON文件中获取数据,但我无法使用C#和Newtonsoft获取数据。有人能帮我解决这个问题吗?数据来自汤森路透API。我试过其他文章,但总是失败。
JSON:
{
"RequestKey": {
"NameType": "RIC",
"Service": "IDN",
"Name": "AEDMYR=R"
},
"QoS": {
"TimelinessInfo": {
"TimeInfo": 0,
"Timeliness": "REALTIME"
},
"RateInfo": {
"TimeInfo": 0,
"Rate": "TICK_BY_TICK"
}
},
"Status": {
"StatusMsg": "OK",
"StatusCode": 0
},
"Fields": {
"Field": [
{
"Name": "HIGH_1",
"DataType": "Double",
"Double": 117.3428
},
{
"Name": "LOW_1",
"DataType": "Double",
"Double": 117.0143
},
{
"Name": "YRHIGH",
"DataType": "Double",
"Double": 121.8861
},
{
"Name": "YRLOW",
"DataType": "Double",
"Double": 95.205
},
{
"Name": "HIGH_TIME",
"DataType": "Utf8String",
"Utf8String": "03:33"
},
{
"Name": "LOW_TIME",
"DataType": "Utf8String",
"Utf8String": "02:55"
},
{
"Name": "BKGD_REF",
"DataType": "Utf8String",
"Utf8String": "UAEDir/MalayRgt"
},
{
"Name": "GEN_TEXT16",
"DataType": "Utf8String",
"Utf8String": "AED vs MYR"
},
{
"Name": "GV2_TEXT",
"DataType": "Utf8String",
"Utf8String": "AEDMYR"
},
{
"Name": "VALUE_TS1",
"DataType": "Utf8String",
"Utf8String": "08:48:00"
},
{
"Name": "VALUE_TS2",
"DataType": "Utf8String",
"Utf8String": "08:47:01"
},
{
"Name": "VALUE_TS3",
"DataType": "Utf8String",
"Utf8String": "08:46:01"
},
{
"Name": "MONTH_HIGH",
"DataType": "Double",
"Double": 118.0954
},
{
"Name": "MONTH_LOW",
"DataType": "Double",
"Double": 114.0702
},
{
"Name": "WEEK_HIGH",
"DataType": "Double",
"Double": 117.3428
},
{
"Name": "WEEK_LOW",
"DataType": "Double",
"Double": 116.8654
},
{
"Name": "SCALING",
"DataType": "Utf8String",
"Utf8String": "100"
},
{
"Name": "CF_ASK",
"DataType": "Double",
"Double": 117.4272
},
{
"Name": "CF_BID",
"DataType": "Double",
"Double": 117.2443
},
{
"Name": "CF_CLOSE",
"DataType": "Double",
"Double": 116.7792
},
{
"Name": "CF_DATE",
"DataType": "Utf8String",
"Utf8String": "23 DEC 2015"
},
{
"Name": "CF_HIGH",
"DataType": "Double",
"Double": 117.3428
},
{
"Name": "CF_LAST",
"DataType": "Double",
"Double": 117.2443
},
{
"Name": "CF_LOW",
"DataType": "Double",
"Double": 117.0143
},
{
"Name": "CF_NETCHNG",
"DataType": "Double",
"Double": 0.4651
},
{
"Name": "CF_OPEN",
"DataType": "Double",
"Double": 116.7792
},
{
"Name": "CF_SOURCE",
"DataType": "Utf8String",
"Utf8String": "ThomRtrs"
},
{
"Name": "CF_TICK",
"DataType": "Utf8String",
"Utf8String": "⇧"
},
{
"Name": "CF_TIME",
"DataType": "Utf8String",
"Utf8String": "08:48:00"
},
{
"Name": "CF_NAME",
"DataType": "Utf8String",
"Utf8String": "UAEDir/MalayRgt"
},
{
"Name": "BQOS",
"DataType": "Utf8String",
"Utf8String": "0"
},
{
"Name": "PQOS",
"DataType": "Utf8String",
"Utf8String": "RT"
},
{
"Name": "ConcreteService",
"DataType": "Utf8String",
"Utf8String": "IDN_FD3"
},
{
"Name": "MSG_TYPE",
"DataType": "Int32",
"Int32": 0
},
{
"Name": "REC_STATUS",
"DataType": "Int32",
"Int32": 0
}
]
}
}
反序列化代码:
var result = "";
using (var objStreamReader = new StreamReader("RetrieveItem_Response_2.json"))
{
result = objStreamReader.ReadToEnd();
}
JObject joResponse = JObject.Parse(result);
JArray joArray = (JArray)(joResponse["RetrieveItem_Response_2"]["ItemResponse"]);
List<Rootobject> objRootobject = JsonConvert.DeserializeObject<List<Rootobject>>(joArray.ToString());
C#类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Rootobject
{
public Requestkey RequestKey { get; set; }
public Qos QoS { get; set; }
public Status Status { get; set; }
public Fields Fields { get; set; }
}
public class Requestkey
{
public string NameType { get; set; }
public string Service { get; set; }
public string Name { get; set; }
}
public class Qos
{
public Timelinessinfo TimelinessInfo { get; set; }
public Rateinfo RateInfo { get; set; }
}
public class Timelinessinfo
{
public int TimeInfo { get; set; }
public string Timeliness { get; set; }
}
public class Rateinfo
{
public int TimeInfo { get; set; }
public string Rate { get; set; }
}
public class Status
{
public string StatusMsg { get; set; }
public int StatusCode { get; set; }
}
public class Fields
{
public Field[] Field { get; set; }
}
public class Field
{
public string Name { get; set; }
public string DataType { get; set; }
public float Double { get; set; }
public string Utf8String { get; set; }
public int Int32 { get; set; }
}
看起来您已经正确定义了所有的类。要反序列化JSON,只需要执行以下操作:
Rootobject obj = JsonConvert.DeserializeObject<Rootobject>(result);
您可以像这样转储字段值:
foreach (Field f in obj.Fields.Field)
{
string value;
switch (f.DataType)
{
case "Utf8String": value = f.Utf8String; break;
case "Double": value = f.Double.ToString(); break;
case "Int32": value = f.Int32.ToString(); break;
default: value = "(unknown)"; break;
}
Console.WriteLine(f.Name + ": " + value);
}
Fiddle:https://dotnetfiddle.net/JD3jPD