[英]Converting JSON string to C# object/array
I am looking to convert the following data into ac# object or array in which i can display each item (eventually to be displayed in a list view). 我希望将以下数据转换为ac#对象或数组,在其中可以显示每个项目(最终将在列表视图中显示)。
the json itself looks like this: json本身看起来像这样:
[
{
"commonName": "uni_comp_4",
"processorID": "BFEFBDEB001201"
},
{
"commonName": "lib_comp_12",
"processorID": "BFEFBDEB004323"
}
]
I have looked here for help however I think I may have to take another approach as my system is slightly different. 我一直在这里寻求帮助,但是由于系统略有不同,我认为我可能不得不采取另一种方法。
I'm using a class: 我正在使用课程:
public class API_Response
{
public bool IsError { get; set; }
public string ErrorMessage { get; set;
public dynamic ResponseData { get; set; }
}
for data carrying. 用于数据携带。 My JSON "data" is as shown above however I have been having issues deserialising this.
我的JSON“数据”如上所述,但是我在反序列化时遇到了问题。
Initially, I tried: 最初,我尝试过:
API_Response r = JsonConvert.DeserializeObject<API_Response>(response);
and I'm able to see the JSON string with MessageBox.show(r.ResponseData). 而且我可以使用MessageBox.show(r.ResponseData)看到JSON字符串。 Which inherently is not deserialised.
本质上不会反序列化。
Additionally, I have tried declaring the following in the same method: 此外,我尝试用相同的方法声明以下内容:
public class RootObject
{
public string commonName { get; set; }
public string processorID { get; set; }
}
with no luck in displaying this data individually (or at all). 不能单独(或完全)显示此数据。
Essentially, I'm trying to put class API_Response's "ResponseData" into an object and I'm having difficulty. 本质上,我试图将类API_Response的“ ResponseData”放入对象中,但遇到了困难。
Shouldn't you deserialize into RootObject[]
, rather than API_Response
? 您不应该反序列化为
RootObject[]
,而不是反序列化为API_Response
吗? Also, depending on the settings you may need to have C# variables in Pascal casing, that is CommonName
and ProcessorID
另外,根据设置,您可能需要在Pascal大小写中包含C#变量,即
CommonName
和ProcessorID
You are trying to deserialize the object with the class which doesn't have elements mentioned in the JSON. 您正在尝试使用不包含JSON中提到的元素的类来反序列化对象。 Try to put
List<RootObject>
because your JSON contains the list of the RootObject
class that you have created. 尝试放入
List<RootObject>
因为您的JSON包含您已创建的RootObject
类的列表。 Try the below solution if it works for you. 如果适合您,请尝试以下解决方案。
JsonConvert.DeserializeObject<List<RootObject>>(response);
just use a decoder to decode the message 只是使用解码器来解码消息
Struct PCS: Decodable
{
let commonName : String?
let proccesorID : String?
}
var pcs = [PCS]()
put this in viewdidload 把它放在viewdidload中
func parseData(){
let jsonUrlString = "Your_API_URL"
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) {(data, response, err) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
pcs = try decoder.decode([Course].self, from: data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch let jsonErr{
print("error", jsonErr)
}
}.resume()
in tableView call the data like this 在tableView中像这样调用数据
pcs[indexPath.row].commonName
public class RootObject{
public string commonName { get; set; }
public string processorID { get; set; }
}
public class API_Response
{
public bool IsError { get; set; }
public string ErrorMessage { get; set; }
public RootObject[] ResponseData { get; set; }
}
API_Response r = JsonConvert.DeserializeObject<API_Response>(response);
I can then use this format for calling the data 然后,我可以使用这种格式来调用数据
MessageBox.Show("" + r.ResponseData[0].commonName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.