读取嵌套数组中的值。 JSON

时间:2017-02-08 19:59:59

标签: c# json

至少我认为它叫什么? 最后,我想用读取值填充变量,但是现在我正在使用控制台来获取值。 这是C#,我想要做的是从文件中读取JSON,然后输出每个" row" 我能够输出"外部"值,即不属于嵌套数组(FieldList)的值。 所以下面的代码我得到了 251037 406

using (StreamReader r = new StreamReader("c:\\temp\\crms.json"))
        {
            string json = r.ReadToEnd();
            dynamic array = JsonConvert.DeserializeObject(json);
            foreach (var item in array)
            {
                Console.WriteLine("{0} {1}", item.RecordProcessID, item.CallProcessID);
            }
            Console.ReadLine();
        }

但是如果我尝试获取item.Firstname和item.Surname我什么也得不到。 这是JSON的一个子集,因此您可以看到数据布局...如何读取FieldList数组中的哈希值?

[{"RecordProcessID":251037,
"CallProcessID":406,
"FieldList":[{"fieldName":"DateofCall",
"fieldValue":"1/02/2017"
},
{"fieldName":"TimeOfCall",
"fieldValue":"17:14:54"
},
{"fieldName":"Group",
"fieldValue":"Control"
},
{"fieldName":"OperatorName",
"fieldValue":"Jamie"
},
{"fieldName":"Reference",
"fieldValue":"251037"
},
{"fieldName":"RequestType",
"fieldValue":"(Secured)"
},
{"fieldName":"23773",
"fieldValue":"Blue"
},
{"fieldName":"23774",
"fieldValue":"9809"
},
{"fieldName":"FirstName",
"fieldValue":"Jane"
},
{"fieldName":"Surname",
"fieldValue":"Smith"
},
{"fieldName":"CallersAddress",
"fieldValue":"29 Ave,"
},
{"fieldName":"37527",
"fieldValue":"Yes I have done this"
},
{"fieldName":"CallersPhone",
"fieldValue":"123"
},
{"fieldName":"CallersMobile",
"fieldValue":""
},
{"fieldName":"Problemaddress",
"fieldValue":"29 Ave"
},
{"fieldName":"39081",
"fieldValue":""
},
{"fieldName":"CallDetails",
"fieldValue":"Secured"
},
{"fieldName":"PagedTime",
"fieldValue":"1718"
},
{"fieldName":"ReturnCallTime",
"fieldValue":"1721"
},
{"fieldName":"JobAcceptedBy",
"fieldValue":"Fred"
},
{"fieldName":"ProposedActions",
"fieldValue":"Will go have a look"
}
]
},

更新 - 这是我的最终解决方案

string fieldname = "";
        string fieldvalue = "";
        Dictionary<string, string> myDic = new Dictionary<string, string>(2);

        using (StreamReader r = new StreamReader("c:\\temp\\crms.json"))
        {
            string json = r.ReadToEnd();
            dynamic array = JsonConvert.DeserializeObject(json);

            foreach (dynamic item in array)
            {
                myDic.Clear();
                foreach (var field in item.FieldList)
                {
                    fieldname = field.fieldName;
                    fieldvalue = field.fieldValue;
                    myDic.Add(fieldname, fieldvalue);
                }
                insertNewRecord(myDic["FirstName"], myDic["Surname"]);
            }


        }


    }
    public static void insertNewRecord(string firstName, string surname)
    {
        Console.WriteLine("{0} {1}", firstName, surname);
        Console.ReadLine();
    }

2 个答案:

答案 0 :(得分:0)

你只需要通过列表枚举并找到你想要的字段

    string firstName, surname;
    foreach(dynamic field in item.FieldList)
    {
        if (field.fieldName == "FirstName")
        {
             firstName = field.fieldValue
        }
        elseif (field.fieldName == "Surname")
        {
             surname = field.fieldValue
        } 
    }
    Console.WriteLine(firstName + ", " + surname);

答案 1 :(得分:0)

我创建并测试了以下可以使用的代码:

public class SampleListItem
    {
        public string fieldName { set; get; }
        public string fieldValue { set; get; }
    }

    public class Sample 
    { 
        public int RecordProcessID { set; get; }
        public int CallProcessID {set; get; }
        public List<SampleListItem> FieldList { set; get; }
    }
    public ActionResult Index()
    {
       using (StreamReader r = new StreamReader("C:\\temp\\crms.json"))
        {
            string json = r.ReadToEnd();
            List<Sample> principalArray = JsonConvert.DeserializeObject<List<Sample>>(json);
            foreach (var principalItem in principalArray)
            {
                Console.WriteLine("{0} {1}", principalItem.RecordProcessID, principalItem.CallProcessID);

                foreach (var field in principalItem.FieldList)
                {
                    Console.WriteLine("{0} {1}", field.fieldName, field.fieldValue);

                }
            }
            Console.ReadLine();
        }
    }

字段名称是“fieldName”,而不是“Firstname”