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

  • jezhjezh Member, Xamarin Team Xamurai
    edited January 13

    unfortunately I cant change the json itself

    We can use json2csharp to convert your json to the following code:

        public class Current    {
            public long time { get; set; } 
        }
    
        public class Past    {
            public string settings { get; set; } 
            public int method { get; set; } 
            public string curve { get; set; } 
            public long time { get; set; } 
            public int d { get; set; } 
        }
    
        public class Root    {
            public List<Current> current { get; set; } 
            public List<Past> past { get; set; } 
            public List<object> notes { get; set; } 
        }
    

    What's your problem? Did you have any trouble somewhere?


    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 ✭✭

    Sorry, dont know why this got posted twice :(
    I used json2csharp to convert the json, however I can not display any information from it :(
    https://forums.xamarin.com/discussion/187025/newbie-json#latest

  • jezhjezh Member, Xamarin Team Xamurai

    There are several problems:

    1.According to the json string you posted, the outermost structure of the data is model(Monkey) not a list, so you should use the following code:

       var monkey = JsonConvert.DeserializeObject<Monkey>(json);
    

    2.The propety settings is a field in model Past and and Past is contained in List<Past> past;, if you want to show a list, you can use Listview or CollectionView to achieve this.

    You can check the official sample here to find how to display a list: https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-listview-customcells/


    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 :)

  • XamNewbieXamNewbie Member ✭✭
    edited January 15

    Probably a really dumb way to do it but I managed to get the info by

                    Convert.ToString(json);
                    int s = json.IndexOf("settings") - 2;
                    int f = json.IndexOf("notes") - s;
    
                    json = json.Substring(s, f-3);```
    and 
    
    
    public partial class Monkeys   {
        public string settings { get; set; } 
        public int method { get; set; } 
        public string curve { get; set; } 
        public long time { get; set; } 
        public int d { get; set; } 
    }
    

    ```

    lol and all this was supposed to be the easy part of my awesome app :)

  • jezhjezh Member, Xamarin Team Xamurai

    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."

    For this, you can try to change the data type to float or double.

  • jezhjezh Member, Xamarin Team Xamurai

    Convert.ToString(json);
    int s = json.IndexOf("settings") - 2;
    int f = json.IndexOf("notes") - s;
    json = json.Substring(s, f-3);

    I don't think it's a good idea. Since we can convert the json to corresponding model and fields. Why do you want to get it field by field?

  • XamNewbieXamNewbie Member ✭✭

    I changed it to float and it came up with an error about it not being a list, I did the above just to strip the json of the multipart bits leaving me with just
    {"settings":"61","method":4,"curve":"F","time":1610470723659,"d":1}
    Which then just needs one function to get the values, I know its a silly way to do it, however im new to this and im finding all this soooo confusing, im not sure if I should try Kotlin with android studio instead, I have programmed before using old VS with visual basic, C++ and then php etc, I am just finding Xamarin really really difficult

  • jezhjezh Member, Xamarin Team Xamurai

    Hi @XamNewbie ,It doesn't matter. Maybe it's a little difficult at first, but if you try to overcome some of the first problems, you will find that Xamarin is not as difficult as you think.

  • jezhjezh Member, Xamarin Team Xamurai

    Hi @XamNewbie , have you resolved your problem?

Sign In or Register to comment.