need help with deserializing a json file

DiscordGoat

Member
Joined
Jan 30, 2022
Messages
8
Programming Experience
1-3
I am coding with my first API, and i got through most of the thinking and coding alright, but ive hit a snag with this part, and its really making me lose my cool. i keep getting a variation of Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path 'data', line 1, position 22.'and occasionally it would just say "valmodel.data" meaning the ToString isnt working with the right data i think? any help is useful, feedback appreciated. learning fast is key. i can link the json if i need to as well [1]:
1643586409285.png
 
nevermind, this is what is in the Data_JSON
JSON:
{
    "status":200,
    "data":
    {
        "currenttier":9,
        "currenttierpatched":"Silver 1",
        "ranking_in_tier":78,
        "mmr_change_to_last_game":19,
        "elo":678,
        "name":"DiscordGoat",
        "tag":"6969"
    }
}
Did you know, you can copy the json and in VS use Edit > Paste Special > Paste Json as Classes
This will give you a Rootobject that you can deserialize. In this case it will have status and data properties, and a Data class. You may have to edit the generated classes, especially if you get different data for different requestes, but it will often be a good starting point.
 
Welcome to the forum. In the future, please post code and data as text in code tags instead of screenshots. It is very hard to read screenshots on small devices, as well as, hampers users who may want to help you, but now you are forcing them to have to re-type code or data back in instead of just being able to copy and paste the relevant chuncks.
 
What does the data that comes back from the web service look like? What are the contents of Data_JSON?
 
i will say that ive been messing with it for a few minutes so it looks a bit different, but heres the relevent code
C#:
namespace ValStats
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        private async void button1_Click(object sender, EventArgs e)
        {
            WebRequest request = HttpWebRequest.Create("[URL]https://api.henrikdev.xyz/valorant/v1/mmr/eu/DiscordGoat/6969[/URL]"); //request the data
            WebResponse response = request.GetResponse(); // recieve data
            StreamReader reader = new StreamReader(response.GetResponseStream()); //read the data
            String Data_JSON = reader.ReadToEnd(); //store the data


            JavaScriptSerializer serializer = new JavaScriptSerializer();
            RootObject root = serializer.Deserialize<RootObject>(Data_JSON);
            foreach (var item in root.Data)
            {
                //trying to find a way to display the deserialized code as a string
            }
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }

    public class RootObject
    {
        public List<RootObjects> Data { get; set; }
    }

    public class RootObjects
    {
            public static int currenttier { get; set; } //number rank
            public string currenttierpatched { get; set; } //rank
            public int ranking_in_tier { get; set; } //rr
            public int mmr_change_to_last_game { get; set; } //+/- to rr
            public int elo { get; set; } //elo
            public string name { get; set; } //name
            public string tag { get; set; } //tag
      

    }
}
 
Last edited by a moderator:
ah. youve revealed the problem. this is what bounces back from the web server
System.Net.HttpWebResponse
at least with a ToString. any ideas on how to make it more substantial ?
 
nevermind, this is what is in the Data_JSON
JSON:
{
    "status":200,
    "data":
    {
        "currenttier":9,
        "currenttierpatched":"Silver 1",
        "ranking_in_tier":78,
        "mmr_change_to_last_game":19,
        "elo":678,
        "name":"DiscordGoat",
        "tag":"6969"
    }
}
 
Last edited by a moderator:
so, its getting the data, its just not deserializing it
That's because your RootObject is not the actual root object of the JSON that is sent to you. It is the data element of the JSON that you got. Furthermore, you are asking to deserialize a list of those RootObject's but that JSON there shows that the data element only contains a single object, not an array or list of objects.
 
That's because your RootObject is not the actual root object of the JSON that is sent to you. It is the data element of the JSON that you got. Furthermore, you are asking to deserialize a list of those RootObject's but that JSON there shows that the data element only contains a single object, not an array or list of objects.
how should i go about repairing that
 
so, some notes.
i AM GETing the data, i just cant seem to deserialize it. can someone tell me what obvious mistake i made so i can move on with my code?
Parameters.cs
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ValStats
{
 
        public class JsonParser
        {
            public string tag { get; set; } //tag
            public string name { get; set; } //name                    
            public int elo { get; set; } //elo
            public int currenttier { get; set; } //number rank
            public string currenttierpatched { get; set; } //rank
            public int ranking_in_tier { get; set; } //rr
            public int mmr_change_to_last_game { get; set; } //+/- to rr

            public List<string> Ref { get; set; }
        
        
        

        public override string ToString()
        {
                return String.Format("FetchedInfo: \n\tID: {0}, Name: {1}, Elo: {3}", tag, name, elo, string.Join(",", Ref.ToArray()));
        }

    }
 
}
FORM1.cs
C#:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;


namespace ValStats
{
 

 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


    
        private async void button1_Click(object sender, EventArgs e)
        {
        
                WebRequest request = HttpWebRequest.Create("[URL]https://api.henrikdev.xyz/valorant/v1/mmr/eu/DiscordGoat/6969[/URL]"); //request the data
                WebResponse response = request.GetResponse(); // recieve data
                StreamReader reader = new StreamReader(response.GetResponseStream()); //read the data
                String Data_JSON = reader.ReadToEnd(); //store the data

                string Data_JSONSerialized = JsonConvert.SerializeObject(Data_JSON);
            
            
            

            JsonParser result = JsonConvert.DeserializeObject<JsonParser>(Data_JSONSerialized);
        

        }

    
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        
        }
    }

 
}
ERROR message:
1643622326505.png
 

Attachments

  • 1643622465100.png
    1643622465100.png
    21 KB · Views: 7
Last edited by a moderator:
See post #8.
 
In the future, please don't open a new thread when you already have one about essentially the same topic.
 
Back
Top Bottom