How to display JSON content? Here are the Objects in my JSON

Posted 3 years ago by Alama
Edited 3 years ago
0

I'm trying to display a full skin for an application, the skin is made up of lots of sprites in a PNG SpritesSheet. I have the JSON which contains the list of coordinates of all the sprites in Object format, secondly, I will try to capture each sprite using the coordinates and display them. For now, I'm only trying to parse the JSON and display the Objects list.I am using System.Net.Http.Json 3.2. installed with Nuget.
But, for the moment, nothing work, Nothing items is displayed.

Here my actualy code:

@page "/fetchdata"
@inject HttpClient Http

<h1>JSON Sprites Sheet Parser</h1>

<p>@items <br></p>

@code
{
    private void LoadJson()
    {
        using (StreamReader r = new StreamReader("root/Shared/Skining/sprites_sheet.json"))
        {
            string json = r.ReadToEnd();
            List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);



            foreach (var item in items)
            {

            }

        }
    }
}

And here is a sample of my JSON, it looks like a "frame" collection with a list of objects in it.

{"frames": {

"Bitmap 1":
{
	"frame": {"x":0,"y":1936,"w":100,"h":100},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":100,"h":100},
	"sourceSize": {"w":100,"h":100}
},
"Bitmap 10":
{
	"frame": {"x":366,"y":1882,"w":51,"h":51},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":51,"h":51},
	"sourceSize": {"w":51,"h":51}
},
"Bitmap 11":
{
	"frame": {"x":240,"y":1815,"w":68,"h":73},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":68,"h":73},
	"sourceSize": {"w":68,"h":73}
},
  • 0

    Give this a try, Alama

    @page "/fetchdata"
    
    @using System.IO
    @using Newtonsoft.Json
    
    <h1>JSON Sprites Sheet Parser</h1>
    
    <p>@val</p>
    
    <button @onclick="@(() => LoadJson())">Show Json</button>
    
    @code
    {
        string val;
    
        private void LoadJson()
        {
            using (StreamReader r = new StreamReader("Shared/Skining/sprites_sheet.json"))
            {
                string json = r.ReadToEnd();
                var obj = JsonConvert.DeserializeObject(json);
                var jObject = Newtonsoft.Json.Linq.JObject.Parse(obj.ToString());
    
                val = jObject.ToString();
            }
        }
    }

    And your JSON

    {
      "frames": {
    
        "Bitmap 1": {
          "frame": {
            "x": 0,
            "y": 1936,
            "w": 100,
            "h": 100
          },
          "rotated": false,
          "trimmed": false,
          "spriteSourceSize": {
            "x": 0,
            "y": 0,
            "w": 100,
            "h": 100
          },
          "sourceSize": {
            "w": 100,
            "h": 100
          }
        },
        "Bitmap 10": {
          "frame": {
            "x": 366,
            "y": 1882,
            "w": 51,
            "h": 51
          },
          "rotated": false,
          "trimmed": false,
          "spriteSourceSize": {
            "x": 0,
            "y": 0,
            "w": 51,
            "h": 51
          },
          "sourceSize": {
            "w": 51,
            "h": 51
          }
        },
        "Bitmap 11": {
          "frame": {
            "x": 240,
            "y": 1815,
            "w": 68,
            "h": 73
          },
          "rotated": false,
          "trimmed": false,
          "spriteSourceSize": {
            "x": 0,
            "y": 0,
            "w": 68,
            "h": 73
          },
          "sourceSize": {
            "w": 68,
            "h": 73
          }
        }
      }
    }
    Posted 3 years ago by selliott
Someone is typing...

Post a Reply

You must be logged in to add a new post.
Number of online users: 1
An error has occurred. This application may no longer respond until reloaded. Reload 🗙