首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对象内部的json数组使用c#或java有条件地读取数据。

对象内部的json数组使用c#或java有条件地读取数据。
EN

Stack Overflow用户
提问于 2022-10-24 17:10:34
回答 2查看 59关注 0票数 0

我有个json:

代码语言:javascript
运行
复制
    {
    "data":[
        {
            "id": "Character_Alien",
            "name": "Trespasser",
            "description": "Invader",
            "type": {
                "value": "outfit",
                "displayValue": "Outfit",
                "backendValue": "AthenaCharacter"
            },
            "rarity": {
                "value": "uncommon",
                "displayValue": "Uncommon"
            },
            "series": null,
            "set": null,
            "introduction": {
                "chapter": "2022",
                "text": "Introduced in 2022",
                "backendValue": 22
            },
            "images": {
                "smallIcon": "https://smallimage.png",
                "icon": "https://image.png",
                "featured": "https://imagebig.png",
                "other": null
            },
            "variants": null,
            "searchTags": null,
            "metaTags": null
        },
        {
            "id": "Character_Knowing",
            "name": "Sinister",
            "description": "He holds your fate.",
            "type": {
                "value": "outfit",
                "displayValue": "Outfit",
                "backendValue": "AthenaCharacter"
            },
            "rarity": {
                "value": "rare",
                "displayValue": "Rare",
                "backendValue": "EFortRarity::Rare"
            },
            "series": null,
            "set": {
                "value": "Malice",
                "text": "Path.",
                "backendValue": "GripHate"
            },
            "introduction": {
                "chapter": "2021",
                "backendValue": 22
            },
            "images": {
                "smallIcon": "https://smallimage.png",
                "icon": "https://image.png",
                "featured": "https://imagebig.png",
                "other": null
            },
            "variants": null,
            "searchTags": null,
            "metaTags": null
        }
    ]
}

它是一个JSON 50000+行长,这只是JSON的两个对象。我想得到“名称”,“图像”和“稀有”的价值,只有当"displayValue“是”服装“。有很多不同的displayValues,所以我想先过滤装备的价值,然后我可以知道如何过滤其他的。

我想用C#来做这件事,但如果可以用Java做得更容易,我也可以在那里做(我对这两个方面都有基础知识)

我想到了这一点:

代码语言:javascript
运行
复制
Foreach object in the json
If the object.displayValue = outfit then
string name = object.name
string imagesmall = object.imagesmall
string image = object.image
string imagebig = object.imagebig
String rariry = object.rarity

如果可以这样做的话,我想为每一套衣服生成一个图像,其中的名字和稀缺性作为文本。

任何链接到类似的问题将不胜感激,这是第三天,我正在寻找如何做到这一点,这是我的最后一招。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-24 18:07:29

.Net 6(不需要外部库):

代码语言:javascript
运行
复制
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Linq;
代码语言:javascript
运行
复制
string json = "....."; // Removed for brevity
var results = ((JsonArray)(JsonNode.Parse(json))["data"])
    .Where(o => (string)(o["type"]["displayValue"]) == "Outfit")
    .Select(o => new 
            {
                Name = (string)o["name"],
                ImageSmall = (string)o["images"]["smallIcon"],
                Image = (string)o["images"]["icon"],
                ImageBig = (string)o["images"]["featured"],
                Rarity = (string)o["rarity"]["value"]
            });
foreach (var x in results) {
    Console.WriteLine(x);
}

输出:

代码语言:javascript
运行
复制
{ Name = Trespasser, ImageSmall = https://smallimage.png, Image = https://image.png, ImageBig = https://imagebig.png, Rarity = uncommon }
{ Name = Sinister, ImageSmall = https://smallimage.png, Image = https://image.png, ImageBig = https://imagebig.png, Rarity = rare }
票数 1
EN

Stack Overflow用户

发布于 2022-10-24 17:48:30

您可以很容易地使用Newtonsoft.Json NuGet来完成这个任务。我在示例中使用了JSON作为字符串。您可以添加相关的源。

如果您有一个更大的具有更多属性的JSON,您可以在Visual中使用过去的特殊选项来创建对象检查这里

代码语言:javascript
运行
复制
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        string json = "{" +
            "\n\"data\":[\n" +
            "{\n\"id\":\"Character_Alien\",\n\"name\": \"Trespasser\",\n\"description\": " +
            "\"Invader\",\n\"type\": {\n\"value\": \"outfit\",\n\"displayValue\": \"Outfit\",\n\"backendValue\": " +
            "\"AthenaCharacter\"\n},\n\"rarity\": {\n\"value\": \"uncommon\",\n\"displayValue\": \"Uncommon\"\n}" +
            ",\n\"series\": null,\n\"set\": null,\n\"introduction\": {\n\"chapter\": \"2022\",\n\"text\": \"Introduced in 2022\"," +
            "\n\"backendValue\": 22\n}," +
            "\n\"images\": {\n\"smallIcon\": \"https://smallimage.png\",\n\"icon\": \"https://image.png\"," +
            "\n\"featured\": \"https://imagebig.png\",\n\"other\": null\n},\n\"variants\": null,\n\"searchTags\": null," +
            "\n\"metaTags\": null\n},\n{\n\"id\": \"Character_Knowing\",\n\"name\": \"Sinister\"," +
            "\n\"description\": \"He holds your fate.\",\n\"type\": {\n\"value\": \"outfit\",\n\"displayValue\": \"Outfit\"," +
            "\n\"backendValue\": \"AthenaCharacter\"\n}," +
            "\n\"rarity\": {\n\"value\": \"rare\",\n\"displayValue\": \"Rare\",\n\"backendValue\": \"EFortRarity::Rare\"\n}," +
            "\n\"series\": null,\n\"set\": {\n\"value\": \"Malice\",\n\"text\": \"Path.\",\n\"backendValue\": \"GripHate\"\n}," +
            "\n\"introduction\": {\n\"chapter\": \"2021\",\n\"backendValue\": 22\n},\n\"images\": " +
            "{\n\"smallIcon\": \"https://smallimage.png\",\n\"icon\": \"https://image.png\",\n\"featured\": \"https://imagebig.png\"," +
            "\n\"other\": null\n},\n\"variants\": null,\n\"searchTags\": null,\n\"metaTags\": null\n}\n]\n" +
        "}";


        var jsonArr = JsonConvert.DeserializeObject<JsonArr>(json);

        var val = jsonArr.Data.Where(w => w.Type.Value == "outfit").Select(s => new Data
        {
            Name = s.Name,
            Rarity = s.Rarity
        }).ToList();

        Console.WriteLine(json);
        Console.ReadKey();
    }
}


public class JsonArr
{
    [JsonProperty(PropertyName = "data")]
    public List<Data> Data { get; set; }
}
public class Data
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "type")]
    public DataType Type { get; set; }

    [JsonProperty(PropertyName = "rarity")]
    public Rarity Rarity { get; set; }


}
public class DataType
{
    [JsonProperty(PropertyName = "value")]
    public string Value { get; set; }

    [JsonProperty(PropertyName = "displayValue")]
    public string DisplayValue { get; set; }

    [JsonProperty(PropertyName = "backendValue")]
    public string BackendValue { get; set; }
    
}

public class Rarity
{
    [JsonProperty(PropertyName = "value")]
    public string Value { get; set; }

    [JsonProperty(PropertyName = "displayValue")]
    public string  DisplayValue { get; set; }

    [JsonProperty(PropertyName = "backendValue")]
    public string BackendValue { get; set; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74184569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档