Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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#_Json - Fatal编程技术网

C# 如何将多个JSON值解析为字符串数组?

C# 如何将多个JSON值解析为字符串数组?,c#,json,C#,Json,我正在利用pokeapi来了解api,但我面临着这个问题 JSON数据在其父级中有多个子值。我需要解析所有这些子项的值“type” 我尝试只打印它遇到的第一个值,但收到一个错误“int32 array index expected” 然后,我还尝试将我的变量“type”设置为string[]类型,但它破坏了我当前的代码。我得到错误参数3:无法从“string”转换为“string[]” 构建对象 PokeItem PokeItem=new PokeItem(名称:$“{dataObj[“name

我正在利用pokeapi来了解api,但我面临着这个问题

JSON数据在其父级中有多个子值。我需要解析所有这些子项的值“type”

我尝试只打印它遇到的第一个值,但收到一个错误“int32 array index expected”

然后,我还尝试将我的变量“type”设置为string[]类型,但它破坏了我当前的代码。我得到错误参数3:无法从“string”转换为“string[]”

构建对象

PokeItem PokeItem=new PokeItem(名称:$“{dataObj[“name”]}”,
精灵:$“{dataObj[“精灵”][“前端默认”]}”,
类型:$“{dataObj[“类型”][“类型”][“名称”]}”,
pokedexid:$“{dataObj[“id”]}”);
“模型”类

公共字符串名称{get;set;}
公共字符串Sprite{get;set;}
公共字符串[]类型{get;set;}
公共字符串pokedexID{get;set;}
公共PokeItem(字符串名称、字符串精灵、字符串[]类型、字符串pokedexid)
{
名称=名称;
雪碧=雪碧;
类型=类型;
pokedexID=pokedexID;
}
我正在使用的JSON


    "types": [
        {
            "slot": 2,
            "type": {
                "name": "poison",
                "url": "https://pokeapi.co/api/v2/type/4/"
            }
        },
        {
            "slot": 1,
            "type": {
                "name": "bug",
                "url": "https://pokeapi.co/api/v2/type/7/"
            }
        }
    ],

预期结果:能够获取所有“类型”值并将它们添加到dataObj中,以便以后访问它们

实际结果:

错误:应为int32数组索引

参数3:无法从“字符串”转换为“字符串[]”


具体到您发布的
JSON
字符串,您可以参考以下代码片段将字符串反序列化到其各自的组件中。我使用的是
newtonsoftjson
库,这是一个流行的.NET高性能
JSON
框架

由于您的JSON字符串太长,无法在此处发布,因此我将为您提供一个链接,您可以在其中找到JSON字符串及其关联的解析模型。可以找到链接

使用系统;
使用Newtonsoft.Json;
使用System.Collections.Generic;
公共课程
{
公共静态void Main()
{
string json=“URL中的json字符串”;
var response=JsonConvert.DeserializeObject(json);
Console.WriteLine(response.sprites);
控制台。写入线(响应。基本经验);
foreach(响应能力中的var值)
{
Console.WriteLine(value.ability.name);
Console.WriteLine(value.ability.url);
}
}
}
公共课能力2
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共阶级能力
{
公共能力2能力{get;set;}
公共布尔是隐藏的{get;set;}
公共int槽{get;set;}
}
公开课表格
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共类版本
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公营野味
{
公共整数博弈_索引{get;set;}
公共版本版本{get;set;}
}
公共类项目
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共类版本2
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共类VersionDetail
{
公共整数稀有性{get;set;}
公共版本2版本{get;set;}
}
公营直升机
{
公共项项{get;set;}
公共列表版本_详细信息{get;set;}
}
公开课运动2
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共类MoveLearnMethod
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共类版本组
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共类versionGroup详细信息
{
在{get;set;}上的公共整数级
公共移动学习方法移动学习方法{get;set;}
公共版本组版本组{get;set;}
}
公开课运动
{
公共移动2移动{get;set;}
公共列表版本\组\详细信息{get;set;}
}
公共类物种
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共类精灵
{
公共字符串返回\u默认值{get;set;}
公共对象返回{get;set;}
公共字符串返回{get;set;}
公共对象back_shinny_female{get;set;}
公共字符串前面\u默认值{get;set;}
公共对象前面{get;set;}
公共字符串前面{get;set;}
公共对象前\u闪亮\u女性{get;set;}
}
公开课2
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共类统计
{
公共int base_stat{get;set;}
公共int努力{get;set;}
公共Stat2 stat{get;set;}
}
公共类类型2
{
公共字符串名称{get;set;}
公共字符串url{get;set;}
}
公共类类型
{
公共int槽{get;set;}
公共类型2类型{get;set;}
}
公共类根对象
{
公开名单


希望这能有所帮助。

你能给我看一下你完整的JSON字符串吗?你现在发布的这个字符串无效string@RahulSharma我尝试添加所有JSON数据,但它只是使我的浏览器崩溃。如果您使用postman,请通过它查看数据确切地说是什么
dataObj
,以及您是如何创建的?您是否尝试手动解析JSON?为什么不使用类似JSON.NET的东西将其直接反序列化到对象结构中呢?另外,如果您想继续使用dataObj,那么为了读取第一个值(如您所述),我想您可能需要执行类似于
dataObj[“types”][0][“type”][“name”]
的操作,以便引用“types”的第一个索引数组。否则它不知道你在谈论列表中的哪一项。但是如果你想阅读所有类型,你需要做一些更复杂的事情。就像我说的,为什么不使用合适的反序列化程序呢?…为了更简单,像
using System;
using Newtonsoft.Json;
using System.Collections.Generic;


public class Program
{
    public static void Main()
    {
        string json="Your json string from the URL";
        
        var response = JsonConvert.DeserializeObject<RootObject>(json);

        Console.WriteLine(response.sprites);
        Console.WriteLine(response.base_experience);

        foreach(var value in response.abilities)
        {
            Console.WriteLine(value.ability.name);
            Console.WriteLine(value.ability.url);
        }
    }
}

public class Ability2
{
    public string name { get; set; }
    public string url { get; set; }
}

public class Ability
{
    public Ability2 ability { get; set; }
    public bool is_hidden { get; set; }
    public int slot { get; set; }
}

public class Form
{
    public string name { get; set; }
    public string url { get; set; }
}

public class Version
{
    public string name { get; set; }
    public string url { get; set; }
}

public class GameIndice
{
    public int game_index { get; set; }
    public Version version { get; set; }
}

public class Item
{
    public string name { get; set; }
    public string url { get; set; }
}

public class Version2
{
    public string name { get; set; }
    public string url { get; set; }
}

public class VersionDetail
{
    public int rarity { get; set; }
    public Version2 version { get; set; }
}

public class HeldItem
{
    public Item item { get; set; }
    public List<VersionDetail> version_details { get; set; }
}

public class Move2
{
    public string name { get; set; }
    public string url { get; set; }
}

public class MoveLearnMethod
{
    public string name { get; set; }
    public string url { get; set; }
}

public class VersionGroup
{
    public string name { get; set; }
    public string url { get; set; }
}

public class VersionGroupDetail
{
    public int level_learned_at { get; set; }
    public MoveLearnMethod move_learn_method { get; set; }
    public VersionGroup version_group { get; set; }
}

public class Move
{
    public Move2 move { get; set; }
    public List<VersionGroupDetail> version_group_details { get; set; }
}

public class Species
{
    public string name { get; set; }
    public string url { get; set; }
}

public class Sprites
{
    public string back_default { get; set; }
    public object back_female { get; set; }
    public string back_shiny { get; set; }
    public object back_shiny_female { get; set; }
    public string front_default { get; set; }
    public object front_female { get; set; }
    public string front_shiny { get; set; }
    public object front_shiny_female { get; set; }
}

public class Stat2
{
    public string name { get; set; }
    public string url { get; set; }
}

public class Stat
{
    public int base_stat { get; set; }
    public int effort { get; set; }
    public Stat2 stat { get; set; }
}

public class Type2
{
    public string name { get; set; }
    public string url { get; set; }
}

public class Type
{
    public int slot { get; set; }
    public Type2 type { get; set; }
}

public class RootObject
{
    public List<Ability> abilities { get; set; }
    public int base_experience { get; set; }
    public List<Form> forms { get; set; }
    public List<GameIndice> game_indices { get; set; }
    public int height { get; set; }
    public List<HeldItem> held_items { get; set; }
    public int id { get; set; }
    public bool is_default { get; set; }
    public string location_area_encounters { get; set; }
    public List<Move> moves { get; set; }
    public string name { get; set; }
    public int order { get; set; }
    public Species species { get; set; }
    public Sprites sprites { get; set; }
    public List<Stat> stats { get; set; }
    public List<Type> types { get; set; }
    public int weight { get; set; }
}