Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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数组名#_C#_Arrays_Json - Fatal编程技术网

C# 提取JSON数组名#

C# 提取JSON数组名#,c#,arrays,json,C#,Arrays,Json,我试图通过一个API用json.NET反序列化这段json,我只想提取代表每个数组的名称,它们是John、Marie和Bob。是否可以使用foreach或任何类型的方法提取每个数组的名称 以下是我目前的情况: WebRequest request = WebRequest.Create("...API URL..."); WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseSt

我试图通过一个API用json.NET反序列化这段json,我只想提取代表每个数组的名称,它们是John、Marie和Bob。是否可以使用foreach或任何类型的方法提取每个数组的名称

以下是我目前的情况:

WebRequest request = WebRequest.Create("...API URL...");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

string responseFromServer = reader.ReadToEnd();

reader.Close();
response.Close();
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(responseFromServer);


foreach (var item in dict["data"])
{
   string arrayName= Convert.ToString(item["data"]["?array_names?"]);
   Console.WriteLine(arrayName);
}
[编辑:在开始和结束处添加了缺少的{},抱歉]


提前感谢

假设使用有效的json格式-下面的代码将生成结果-

var json = "{'data':{'John':{'id':266,'title':'Good old john','name':'John Shepard','key':'JS'},'Marie':{'id':412,'title':'Helper Marie','name':'Marie Godfather','key':'MG'},'Bob':{'id':23,'title':'Uncle Bob','name':'Bob Plane','key':'BP'}}}";
var t = JObject.Parse(json)["data"];
foreach(JProperty j in t){
   Console.WriteLine(j.Name);
} /*John
    Marie
    Bob*/
建议使用它来处理json数据类型。

var json=“{'data':{'John':{'id':266,'title':'Good old John','name':'John Shepard','key':'JS','Marie':{'id':412,'title':'Helper Marie','name':'Marie教父','key':'MG','Bob':'id','23,'title':'Bob叔叔','name':'Bob Plane','key':'BP}”;
var json = "{'data':{'John':{'id':266,'title':'Good old john','name':'John Shepard','key':'JS'},'Marie':{'id':412,'title':'Helper Marie','name':'Marie Godfather','key':'MG'},'Bob':{'id':23,'title':'Uncle Bob','name':'Bob Plane','key':'BP'}}}";

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(json);

foreach (var item in dict["data"])
    {
        string arrayName = Convert.ToString(item.Key);
        Console.WriteLine(arrayName);
    }

// John
// Marie
// Bob
var jss=新的JavaScriptSerializer(); var dict=jss.Deserialize(json); foreach(dict[“数据”]中的var项) { string arrayName=Convert.ToString(item.Key); Console.WriteLine(arrayName); } //约翰 //玛丽 //鲍勃
项[“数据”][0]
可能是?至少post有效JSON请尝试字符串arrayName=Convert.ToString(项[0]);这里显示的Json没有数组。发布准确的json。您不能反序列化要反序列化,您的代码中应该有一个重复的对象类型Amit Kumar Ghosh和Anand Systemix->不能对“System.Collections.Generic.KeyValuePair”类型的表达式应用带[]的索引这正是它。非常感谢你!这不是OP要求的。这不会打印“John,Marie,Bob”你可以试试代码,它会打印“John,Marie,Bob”
var json = "{'data':{'John':{'id':266,'title':'Good old john','name':'John Shepard','key':'JS'},'Marie':{'id':412,'title':'Helper Marie','name':'Marie Godfather','key':'MG'},'Bob':{'id':23,'title':'Uncle Bob','name':'Bob Plane','key':'BP'}}}";

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(json);

foreach (var item in dict["data"])
    {
        string arrayName = Convert.ToString(item.Key);
        Console.WriteLine(arrayName);
    }

// John
// Marie
// Bob