C# 当我获取json值时,对象引用未设置为对象错误的实例
我正在尝试获取“senses”数组中的“definitions”,无论对象引用是否存在错误。我创建了一个类,我只能获取最多为“results”数组的值,例如(id、language、text和word),但由于错误,我无法访问“lexicalentry”、“entries”和“senses”数组以及这些值 这是我的JSONC# 当我获取json值时,对象引用未设置为对象错误的实例,c#,.net,json,C#,.net,Json,我正在尝试获取“senses”数组中的“definitions”,无论对象引用是否存在错误。我创建了一个类,我只能获取最多为“results”数组的值,例如(id、language、text和word),但由于错误,我无法访问“lexicalentry”、“entries”和“senses”数组以及这些值 这是我的JSON "id":"bird", "metadata":{ "operation":"retrieve", "provider":"Oxford University Press",
"id":"bird",
"metadata":{
"operation":"retrieve",
"provider":"Oxford University Press",
"schema":"RetrieveEntry"
},
"results":[
{
"id":"bird",
"language":"en-gb",
"lexicalEntries":[
{
"entries":[
{
"senses":[
{
"definitions":[
"a warm-blooded egg-laying vertebrate animal distinguished by the possession of feathers, wings, a beak, and typically by being able to fly."
],
"id":"m_en_gbus0097360.006",
"subsenses":[]
},
{
"definitions":[
"a person of a specified kind or character"
],
"id":"m_en_gbus0097360.014"
},
{
"definitions":[
"a young woman or a girlfriend."
],
"id":"m_en_gbus0097360.016"
}
]
}
],
"language":"en-gb",
"lexicalCategory":{
"id":"noun",
"text":"Noun"
},
"text":"bird"
}
],
"type":"headword",
"word":"bird"
}
],
"word":"bird"
}
这是我的课
class WordDefinition
{
public RootObject rootObject { get; set; }
public Metadata metadata { get; set; }
public List<Result> results { get; set; }
public List<LexicalEntry> lexicalEntries { get; set; }
public List<Entry> entries { get; set; }
public List<Sens> senses { get; set; }
public List<Subsens> subsenses { get; set; }
public LexicalCategory lexicalCategory { get; set; }
public class Metadata
{
public string operation { get; set; }
public string provider { get; set; }
public string schema { get; set; }
}
public class Subsens
{
public List<string> definitions { get; set; }
public string id { get; set; }
}
public class Sens
{
public List<string> definitions { get; set; }
public string id { get; set; }
public List<Subsens> subsenses { get; set; }
}
public class Entry
{
public List<Sens> senses { get; set; }
}
public class LexicalCategory
{
public string id { get; set; }
public string text { get; set; }
}
public class LexicalEntry
{
public List<Entry> entries { get; set; }
public string language { get; set; }
public LexicalCategory lexicalCategory { get; set; }
public string text { get; set; }
}
public class Result
{
public string id { get; set; }
public string language { get; set; }
public List<LexicalEntry> lexicalEntries { get; set; }
public string type { get; set; }
public string word { get; set; }
}
public class RootObject
{
public string id { get; set; }
public Metadata metadata { get; set; }
public List<Result> results { get; set; }
public string word { get; set; }
}
}
classworddefinition
{
公共根对象根对象{get;set;}
公共元数据{get;set;}
公共列表结果{get;set;}
公共列表{get;set;}
公共列表项{get;set;}
公共列表{get;set;}
公共列表子类{get;set;}
公共词汇分类词汇分类{get;set;}
公共类元数据
{
公共字符串操作{get;set;}
公共字符串提供程序{get;set;}
公共字符串架构{get;set;}
}
公共类子集
{
公共列表定义{get;set;}
公共字符串id{get;set;}
}
公共类传感器
{
公共列表定义{get;set;}
公共字符串id{get;set;}
公共列表子类{get;set;}
}
公开课入学
{
公共列表{get;set;}
}
公共类词汇分类
{
公共字符串id{get;set;}
公共字符串文本{get;set;}
}
公共类词汇库
{
公共列表项{get;set;}
公共字符串语言{get;set;}
公共词汇分类词汇分类{get;set;}
公共字符串文本{get;set;}
}
公开课成绩
{
公共字符串id{get;set;}
公共字符串语言{get;set;}
公共列表{get;set;}
公共字符串类型{get;set;}
公共字符串字{get;set;}
}
公共类根对象
{
公共字符串id{get;set;}
公共元数据{get;set;}
公共列表结果{get;set;}
公共字符串字{get;set;}
}
}
及
var test=JsonConvert.DeserializeObject(jsonResponse);
foreach(测试感官中的var测试)
{
MessageBox.Show(testing.definitions[0].ToString());
}
我希望输出必须是results.definitions[0]。但有一个错误:
对象引用未设置为对象错误的实例
…在json中,这里发生了一些事情 1) JSON无效,它应该以
{
字符开头,例如:
{
"id": "bird",
"metadata": {
"operation": "retrieve",
"provider": "Oxford University Press",
"schema": "RetrieveEntry"
},
"results": [
{
"id": "bird",
"language": "en-gb",
"lexicalEntries": [
{
"entries": [
{
"senses": [
{
"definitions": [
"a warm-blooded egg-laying vertebrate animal distinguished by the possession of feathers, wings, a beak, and typically by being able to fly."
],
"id": "m_en_gbus0097360.006",
"subsenses": []
},
{
"definitions": [
"a person of a specified kind or character"
],
"id": "m_en_gbus0097360.014"
},
{
"definitions": [
"a young woman or a girlfriend."
],
"id": "m_en_gbus0097360.016"
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "noun",
"text": "Noun"
},
"text": "bird"
}
],
"type": "headword",
"word": "bird"
}
],
"word": "bird"
}
2) 集合应在类定义中初始化,例如:
classworddefinition
{
公共根对象根对象{get;set;}
公共元数据{get;set;}
公共列表结果{get;set;}=new List();
公共列表lexicalEntries{get;set;}=new List();
公共列表项{get;set;}=new List();
public List senses{get;set;}=new List();
公共列表子列表{get;set;}=new List();
公共词汇分类词汇分类{get;set;}
公共类元数据
{
公共字符串操作{get;set;}
公共字符串提供程序{get;set;}
公共字符串架构{get;set;}
}
公共类子集
{
公共列表定义{get;set;}=new List();
公共字符串id{get;set;}
}
公共类传感器
{
公共列表定义{get;set;}=new List();
公共字符串id{get;set;}
公共列表子列表{get;set;}=new List();
}
公开课入学
{
public List senses{get;set;}=new List();
}
公共类词汇分类
{
公共字符串id{get;set;}
公共字符串文本{get;set;}
}
公共类词汇库
{
公共列表项{get;set;}=new List();
公共字符串语言{get;set;}
公共词汇分类词汇分类{get;set;}
公共字符串文本{get;set;}
}
公开课成绩
{
公共字符串id{get;set;}
公共字符串语言{get;set;}
公共列表lexicalEntries{get;set;}=new List();
公共字符串类型{get;set;}
公共字符串字{get;set;}
}
公共类根对象
{
公共字符串id{get;set;}
公共元数据{get;set;}
公共列表结果{get;set;}=new List();
公共字符串字{get;set;}
}
}
3)JSON在根级别没有senses
元素。可能您打算使用test.results[0]。lexicalEntries[0]。senses
?例如:
var test=JsonConvert.DeserializeObject(jsonResponse);
foreach(测试中的var测试。结果[0]。词汇表[0]。条目[0]。词义)
{
MessageBox.Show(testing.definitions[0].ToString());
}
{
"id": "bird",
"metadata": {
"operation": "retrieve",
"provider": "Oxford University Press",
"schema": "RetrieveEntry"
},
"results": [
{
"id": "bird",
"language": "en-gb",
"lexicalEntries": [
{
"entries": [
{
"senses": [
{
"definitions": [
"a warm-blooded egg-laying vertebrate animal distinguished by the possession of feathers, wings, a beak, and typically by being able to fly."
],
"id": "m_en_gbus0097360.006",
"subsenses": []
},
{
"definitions": [
"a person of a specified kind or character"
],
"id": "m_en_gbus0097360.014"
},
{
"definitions": [
"a young woman or a girlfriend."
],
"id": "m_en_gbus0097360.016"
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "noun",
"text": "Noun"
},
"text": "bird"
}
],
"type": "headword",
"word": "bird"
}
],
"word": "bird"
}