Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用json.NETC获取json对象中的值和键#_C#_Json_Json.net - Fatal编程技术网

C# 使用json.NETC获取json对象中的值和键#

C# 使用json.NETC获取json对象中的值和键#,c#,json,json.net,C#,Json,Json.net,您好,我有一个json,看起来像这样: { "Id": " 357342524563456678", "title": "Person", "language": "eng", "questionAnswer": [ { "4534538254745646.1": { "firstName": "Janet", "questionNumber": "1.1"

您好,我有一个json,看起来像这样:

{
    "Id": " 357342524563456678",
    "title": "Person",
    "language": "eng",
    "questionAnswer": [
        {
            "4534538254745646.1": {
                "firstName": "Janet",
                "questionNumber": "1.1"
            }
        }
    ]
}
现在我已经编写了一些代码,在
questionAnswer
数组中的对象上循环,然后获取对象的名称,即
4534538254745646.1
。现在我试图保存每个项目的键和值,但我只是设法获得值

我该怎么做,这是我的代码:

JToken entireJson = JToken.Parse(json);
JArray inner = entireJson["questionAnswer"].Value<JArray>();


foreach(var item in inner)
{
     JProperty questionAnswerDetails = item.First.Value<JProperty>();
     //This line gets the name, which is fine
     var questionAnswerSchemaReference = questionAnswerDetails.Name;
     var properties = questionAnswerDetails.Value.First;

     //This only gets Janet
     var key = properties.First;
     var value = properties.Last;                                      
}
JToken entireJson=JToken.Parse(json);
JArray inner=entireJson[“questionAnswer”].Value();
foreach(内部的var项目)
{
JProperty questionAnswerDetails=item.First.Value();
//这一行获取名称,这很好
var questionAnswerSchemaReference=questionAnswerDetails.Name;
var properties=questionAnswerDetails.Value.First;
//这只会让珍妮特
var key=properties.First;
var值=属性。最后;
}
因此,目前我只能得到珍妮特,但我还想要名字字段。然后我想把它添加到字典中,比如

Dictionary<string, string> details = new Dictionary<string, string>();
//suedo
foreach(var item in questionAnswerObjects)
details.Add(firstName, Janet);
//And then any other things found below this
Dictionary details=newdictionary();
//苏伊多
foreach(questionAnswerObjects中的变量项)
详细信息。添加(名字,珍妮特);
//还有在下面发现的其他东西吗

下面是获取数组中对象中每个项的键和值的完整代码:

string key = null;
string value = null;

foreach(var item in inner)
{
    JProperty questionAnswerDetails = item.First.Value<JProperty>();

    var questionAnswerSchemaReference = questionAnswerDetails.Name;

    var propertyList = (JObject)item[questionAnswerSchemaReference];

    questionDetails = new Dictionary<string, object>();

    foreach (var property in propertyList)
    {
         key = property.Key;
         value = property.Value.ToString();
    }

    questionDetails.Add(key, value);               
}
string key=null;
字符串值=null;
foreach(内部的var项目)
{
JProperty questionAnswerDetails=item.First.Value();
var questionAnswerSchemaReference=questionAnswerDetails.Name;
var propertyList=(JObject)项[questionAnswerSchemaReference];
questionDetails=新字典();
foreach(propertyList中的var属性)
{
key=property.key;
value=property.value.ToString();
}
问题详情。添加(键、值);
}
我现在可以向字典添加键和值