Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Parse string from URL? C#

So for example if you click Here I am trying to make it so in C# you can get the url for each item such as I want it to return the http image url for icon
Problem is json parser don't really recognize it because of how they add brackets and make it weird.
Looks like fine to me and the go json deseralizer can handle it.
Spoiler (Click to View)


What library are you trying to use?
Lotta blood to let, peace to make, give a fuk about a check
It should recognize it because it's a standard json response. Here's a tool you may find useful http://json2csharp.com/

Code
public class Obtained
{
    public string obtained { get; set; }
    public string type { get; set; }
}

public class Images
{
    public string icon { get; set; }
    public string background { get; set; }
    public string item { get; set; }
}

public class Item
{
    public string itemid { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string capital { get; set; }
    public string type { get; set; }
    public int cost { get; set; }
    public string rarity { get; set; }
    public Obtained obtained { get; set; }
    public Images images { get; set; }
}
[Image: Syr3DlV.png]
(Oct 25, 2018 04:16 AM)BahNahNah Wrote:  Looks like fine to me and the go json deseralizer can handle it.
Spoiler (Click to View)


What library are you trying to use?

I am using newtonsoft to do it, Not sure how that would play in this.
(Oct 25, 2018 06:44 AM)-PUBG- Wrote:  I am using newtonsoft to do it, Not sure how that would play in this.

Imma guess that you havnt make your models correctly in c# then.
Lotta blood to let, peace to make, give a fuk about a check
(Oct 25, 2018 06:47 AM)BahNahNah Wrote:  Imma guess that you havnt make your models correctly in c# then.

I haven't messed with json much

This is what I have so far but not sure the calling for it

Code
namespace GetImage
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Get
    {
        [JsonProperty("itemid")]
        public string Itemid { get; set; }

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

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

        [JsonProperty("capital")]
        public string Capital { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("cost")]
        public long Cost { get; set; }

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

        [JsonProperty("obtained")]
        public Obtained Obtained { get; set; }

        [JsonProperty("images")]
        public Images Images { get; set; }
    }

    public partial class Images
    {
        [JsonProperty("icon")]
        public Uri Icon { get; set; }

        [JsonProperty("background")]
        public Uri Background { get; set; }

        [JsonProperty("item")]
        public Uri Item { get; set; }
    }

    public partial class Obtained
    {
        [JsonProperty("obtained")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long ObtainedObtained { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }
    }

    public partial class Welcome
    {
        public static Welcome[] FromJson(string json) => JsonConvert.DeserializeObject<Welcome[]>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this Welcome[] self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters = {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }

    internal class ParseStringConverter : JsonConverter
    {
        public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);

        public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null) return null;
            var value = serializer.Deserialize<string>(reader);
            long l;
            if (Int64.TryParse(value, out l))
            {
                return l;
            }
            throw new Exception("Cannot unmarshal type long");
        }

        public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
        {
            if (untypedValue == null)
            {
                serializer.Serialize(writer, null);
                return;
            }
            var value = (long)untypedValue;
            serializer.Serialize(writer, value.ToString());
            return;
        }

        public static readonly ParseStringConverter Singleton = new ParseStringConverter();
    }
}


Always get this error

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: Q. Path '', line 0, position 0.'
Haven't touched C# in a while, but if you just do

Code
[Serializable]
internal class Model
{
    public string itemid { get; set; }
    // etc
}
// This might be really wrong, but it's either Model[] or Model but because its an array item i cant remember
JsonConvert.DeserializeObject<Model[]>(data from get request);
Hi
to upgrade to the gigajew user response and offer a solution.

[Image: JtWs5iV.png]

Models of gigajew
Code
public class Item
   {
       public string itemid { get; set; }
       public string name { get; set; }
       public string description { get; set; }
       public string capital { get; set; }
       public string type { get; set; }
       public int cost { get; set; }
       public string rarity { get; set; }
       public Obtained obtained { get; set; }
       public Images images { get; set; }
   }

public class Obtained
   {
       public string obtained { get; set; }
       public string type { get; set; }
   }
public class Images
   {
       public string icon { get; set; }
       public string background { get; set; }
       public string item { get; set; }
   }

api class
Code
   public class Api
   {
       private ObservableCollection<Item> items;

       private const string url = "https://fortnite-public-files.theapinetwork.com/search?query=Skull%20Trooper";

       public Api()
       {
           this.items = new ObservableCollection<Item>();
       }

       public ObservableCollection<Item> getNods()
       {
           return items;
       }

       public void GetItems()
       {
           HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
           request.Method = "GET";
           request.KeepAlive = true;
           request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8";
           request.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
           using (WebResponse response = (HttpWebResponse)await request.GetResponseAsync())
           using (Stream responseStream = response.GetResponseStream())
           using (StreamReader reader = new StreamReader(responseStream))
           {
               
               var data = reader.ReadToEnd();
               
               var root = JsonConvert.DeserializeObject<List<Item>>(data);
               foreach (var item in root)
               {
                   items.Add(new Item
                   {

                       itemid = item.itemid,
                       name = item.name,
                       description = item.description,
                       capital = item.capital,
                       type = item.type,
                       cost = item.cost,
                       rarity = item.rarity,
                       obtained = item.obtained,
                       images = item.images
                   });
               }

           }
       }
   }

usage
Code
api = new Api();
api.GetItems();
grid.DataContext = api.getNods();

xaml
Code
       <DataGrid x:Name="grid" Grid.Row="1" CanUserAddRows="False" AutoGenerateColumns="False" ItemsSource="{Binding}">
           <DataGrid.Columns>
               <DataGridTextColumn Header="itemid" Binding="{Binding Path=itemid }" IsReadOnly="True" Width="50" />
               <DataGridTextColumn Header="name" Binding="{Binding Path=name}" IsReadOnly="True"/>
               <DataGridTextColumn Header="description" Binding="{Binding Path=description}" IsReadOnly="True"/>
               <DataGridTextColumn Header="capital" Binding="{Binding Path=capital}" IsReadOnly="True"/>
               <DataGridTextColumn Header="type" Binding="{Binding Path=type}" IsReadOnly="True" />
               <DataGridTextColumn Header="cost" Binding="{Binding Path=cost}" IsReadOnly="True" />
               <DataGridTextColumn Header="rarity" Binding="{Binding Path=rarity}" IsReadOnly="True" />
               <DataGridTemplateColumn Header="obtained">
                   <DataGridTemplateColumn.CellTemplate>
                       <DataTemplate>
                           <StackPanel>
                               <TextBlock Text="obtained"/>
                               <TextBlock Text="{Binding obtained.obtained}"/>
                               <TextBlock Text="type"/>
                               <TextBlock Text="{Binding obtained.type}"/>
                           </StackPanel>
                       </DataTemplate>
                   </DataGridTemplateColumn.CellTemplate>
               </DataGridTemplateColumn>
               <DataGridTemplateColumn Header="images">
                   <DataGridTemplateColumn.CellTemplate>
                       <DataTemplate>
                           <StackPanel>
                               <TextBlock Text="icon"/>
                               <TextBlock Text="{Binding images.icon}"/>
                               <TextBlock Text="background"/>
                               <TextBlock Text="{Binding images.background}"/>
                               <TextBlock Text="item"/>
                               <TextBlock Text="{Binding images.item}"/>
                           </StackPanel>
                       </DataTemplate>
                   </DataGridTemplateColumn.CellTemplate>
               </DataGridTemplateColumn>

           </DataGrid.Columns>
       </DataGrid>
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
Yeah none of these seem to be getting the URL properly
(Oct 25, 2018 09:27 PM)-PUBG- Wrote:  Yeah none of these seem to be getting the URL properly

code returns all that json contains,and it remains for you to decide what and how you want to use
[Image: xsvadtz.png]
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Oct 26, 2018 02:39 AM)itcorp Wrote:  code returns all that json contains,and it remains for you to decide what and how you want to use
[Image: xsvadtz.png]

Yeah I am not sure not even sure how I would go about getting that into my tool and making it look nice, Is there a way you could send me that project so I can mess around with it.
(Oct 26, 2018 08:45 PM)-PUBG- Wrote:  Yeah I am not sure not even sure how I would go about getting that into my tool and making it look nice, Is there a way you could send me that project so I can mess around with it.

... Am I missing something or did he not just give you everything here: https://hackforums.net/showthread.php?ti...id57765679
???
(Oct 26, 2018 08:45 PM)-PUBG- Wrote:  Yeah I am not sure not even sure how I would go about getting that into my tool and making it look nice, Is there a way you could send me that project so I can mess around with it.

Are you fucking stupid?

He literally gave you the answer on how to do it.

Now you adapt it to your needs, and create your own program.
(Oct 26, 2018 09:03 PM)FarosMoloko Wrote:  Are you fucking stupid?

He literally gave you the answer on how to do it.

Now you adapt it to your needs, and create your own program.

Apologies, But honestly no need to be so toxic when I simply asked.
(Oct 26, 2018 09:06 PM)-PUBG- Wrote:  Apologies, But honestly no need to be so toxic when I simply asked.

Legit this community has influenced me into being toxic.

Sorry for calling you stupid


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by XoggApr 24, 2020 01:14 AM 2 138 Apr 24, 2020 01:14 AM
Last Post: Lindros
[C] by MordiciaNov 15, 2019 07:21 PM 3 211 Nov 15, 2019 07:21 PM
Last Post: Mordicia
[Shell] by ProfitableFlipMay 24, 2019 09:40 AM 1 162 May 24, 2019 09:40 AM
Last Post: Adolf