Forum Xamarin.Forms

Newbie Json

Hello,

Sorry I hope someone can help me with this I think I'm being really dumb and I just can't figure it out, I'm trying to learn xamarin forms (p.s I didn't learn c# before so I'm a complete beginner) I followed an example beginners guide 6 hours long video about a json monkeys example. I've tried to understand the code etc and I've tried everything to modify it for a json that I have and I just can't get it to work, like in the github I tried to use QuickType to get the c# code for my json even tried another site json2csharp and I just can't get it to work with my json (unfortunately I cant change the json itself)
Sample of the json
{"current":[{"time":1610470770087}],"past":[{"settings":"61","method":4,"curve":"F","time":1610470723659,"d":1}],"notes":[]}

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    I use json2csharp to convert the json string to c# class and get the following output

    public class Current{
        public long time;
    }
    
    public class Past{
        public String settings;
        public int method;
        public String curve;
        public long time;
        public int d;
    }
    
    public class Root{
        public List<Current> current;
        public List<Past> past;
        public List<Object> notes;
    }
    

    What's the problem ? ''


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • XamNewbieXamNewbie Member ✭✭
    Hello,
    Thank u :) thats the same function I get however I cant get the values in settings, method etc, will try and more code when I get on the computer
  • XamNewbieXamNewbie Member ✭✭
    edited January 13

    Ok, so the code I used was from https://github.com/jamesmontemagno/xamarin.forms-workshop/tree/master/Part 3 - Navigation

    For the convert part:-

    public class Current{
        public long time;
    }
    
    public class Past{
        public String settings;
        public int method;
        public String curve;
        public long time;
        public int d;
    }
    
    public partial class Monkey{
        public List<Current> current;
        public List<Past> past;
        public List<Object> notes;
    }
    
    

    Also tried

    [JsonProperty("settings")]
            public string Settings { get; set; }
    

    etc

    for the reading part:-

                    var monkeys = JsonConvert.DeserializeObject<List<Monkey>>(json);
    
                    Monkeys.Clear();
                    foreach (var monkey in monkeys)
                    Monkeys.Add(monkey);
    

    or

     var monkeys = JsonConvert.DeserializeObject<Monkey>(json);
    
                    Monkeys.Clear();
                    Monkeys.Add(monkeys);
    

    for the display MainPage.xml
    <Label FontSize="Large" Text="{Binding settings}" />
    Comes up with an error that its not found or something, Binding notes is allowed but when running, says system something etc... lol

    Sorry I think I am just being dumb somewhere, I can get it to work if its a json which doesnt need to be in seperate parts (dont know if that makes any sense) so if it was just
    {"current":[{"time":1610470770087}],"past":[{"settings":"61"}],"notes":[]} I think, dont know if im explaining correctly here :)

  • ColeXColeX Member, Xamarin Team Xamurai
    edited January 14
    1. The root level of the json is an object not a list , so we should deserialize it as an object .

          var monkey = JsonConvert.DeserializeObject<Monkey>(json);
      
    2. settings property belongs to class Past , and Past is contained in List<Past> past; to bind a list to a Label you can use Converter , check here , and in other hand we can create a ListView/CollectionView to show the items(Label) and bind the string on Label .


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • XamNewbieXamNewbie Member ✭✭
    edited January 14

    I think I need to buy a book on c#, I think I got further by:-
    var monkeys = JsonConvert.DeserializeObject<Monkey>(json); var monkeys2 = JsonConvert.DeserializeObject<Past>(json);
    However it complains about d, when it is "0.2", I tried changing it to long, and it says cant something to int64, tried changing it to string and it comes up with "Cant deserialise the current json object because the type requires a json array."

    I think im getting further confused lol, I dont know if I should just ditch all this awkward json work and just go to working with strings, read the json as a string and just search for the parts I need lol, realistically the only info I need from the Json is settings and curve :)

  • ColeXColeX Member, Xamarin Team Xamurai

    when it is "0.2", I tried changing it to long, and it says cant something to int64, tried changing it to string and it comes up with "Cant deserialise the current json object because the type requires a json array."

    Setting its type as float or double should be able to solve the problem .

Sign In or Register to comment.