Forum Xamarin.Forms

How to pass the nest json object parameters to api with httpclient ?

WinsonQiuWinsonQiu USMember ✭✭

I need to pass a complex json object as parameters to api with httpclient, the json format like below:

and the problem is many of these objects are dynamic, for example, the "guid1" under photos will be more than one and the name will be difference:
guid1:{}, guid2{}, guid3{}...

also, the children under inspection_values are dynamic, it will be more than one

so how can I do that? thanks!

Best Answer

  • WinsonQiuWinsonQiu US ✭✭
    Accepted Answer

    ok, I have solved my problem, I just use a Dictionary for the photo object and is ok:

    public class InsInspection
        {
            public InsProperty property { get; set; }
            public Dictionary<string,Guid> photos { get; set; }
            public List<InsInspectionValue> inspection_values { get; set; }
            public InsInspectionItem inspection { get; set; }
        }
    

    anyway, thanks Colex for help!

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    First of all , I suggest you change photos to list rather than object unless the data format is different between guid1 , guid2, guid3. .

    Here we go

    1. Use http://json2csharp.com/ to generate class from json , copy the code into your project.

    2. Populate the dynamic data with photos(same way in inspection_values ) in code hebind.

      RootObject root = new RootObject();
          Inspection inspection = new Inspection();
      
          List<Photo> photos = new List<Photo>();
      
          //populate data
          photos.Add(new Photo() { url = "", lat = "", lng = "", filename = "", caption = "" });
      
          inspection.photos = photos;
          root.inspection = inspection;
      
    3. Serialize object to json string

           string json = JsonConvert.SerializeObject(root);
      

    In this way you can modify your json every time in code behind.

  • WinsonQiuWinsonQiu USMember ✭✭

    thanks for your reply, I also want to make a standand class object, but I can't control the api paramerters, so I have to pass the dynamic key name with guid1, guid2, guid3.... into the api :(

    I also want to use the Dictionary<string, object> so that can dynamic set the json key, but don't know how to let it to a nest object

    and the website http://json2csharp.com/ is great, thanks!

  • WinsonQiuWinsonQiu USMember ✭✭
    Accepted Answer

    ok, I have solved my problem, I just use a Dictionary for the photo object and is ok:

    public class InsInspection
        {
            public InsProperty property { get; set; }
            public Dictionary<string,Guid> photos { get; set; }
            public List<InsInspectionValue> inspection_values { get; set; }
            public InsInspectionItem inspection { get; set; }
        }
    

    anyway, thanks Colex for help!

Sign In or Register to comment.