0

I have a php web service that I have tried to consume and parse data to my textbox in designer(I work on a windows 10 app store application),this is my code:

public sealed partial class MainPage : Page
    {
        public string uriString = "my URL";
        public MainPage()
        {
            this.InitializeComponent();
            Getdata();
        }

        private async void Getdata()
        {
            var http = new HttpClient();
            http.MaxResponseContentBufferSize = Int32.MaxValue;
            System.Collections.ArrayList response = new System.Collections.ArrayList(new string[] { await http.GetStringAsync(uriString) });
            var rootObject = JsonConvert.DeserializeObject<Sponsorises.RootObject>(response); //the error is here
            sponsorname.Text = rootObject.nom; //the name of my textBox
        }
        public class Sponsorises
     {

    internal class RootObject
    {
        public string nom { get; set; }
        public string tel { get; set; }
        public string photo { get; set; }
        public string sponsorise_adrs { get; set; }

    }
}

this is my json code:

 {
success: 1,
message: "sponsorise found!",
total: 1,
sponsorises: [
{
nom: "my third local",
tel: "88888888",
photo: "http://192.168.1.1/1446241709_ab_cart2_bleu2.png",
sponsorise_adrs: "the adress"
}
]
}

I am having problem in converting arraylist response to the string rootObject,have you please any idea thanks for help

4
  • 1
    What is the 'problem'? Any exceptions? Commented Dec 4, 2015 at 16:14
  • I have an error :"can not convert 'System.Collections.arrayList' to string in the line of affecting response to rootObject Commented Dec 4, 2015 at 16:18
  • 1
    That error seems pretty clear to me. What have you tried to solve it? Commented Dec 4, 2015 at 16:20
  • I tried to do this line: List<string> rootObject = JsonConvert.DeserializeObject<Sponsorises.RootObject>(response); Commented Dec 4, 2015 at 16:24

3 Answers 3

1

You may add another class for sponsorises:

internal class RootObject
{
    public string success { get; set; }
    public string message { get; set; }
    public string total { get; set; }
    public List<Sponsorise> sponsorises { get; set; }
}

class Sponsorise
{
    public string nom { get; set; }

    public string tel { get; set; }

    public string photo { get; set; }

    public string sponsorise_adrs { get; set; }
}

And deserialize like so:

var rootObject = JsonConvert.DeserializeObject<RootObject>(response); 
sponsorname.Text = rootObject.sponsorises[0].nom; 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks Sir for your code but If I apply it I get an other error which is: RootObject doesn't contain a definition for nom :(
1

The issue is that JsonConvert.Deserialize() expects a string, not an array list.

This can be done by not casting your web response to ArrayList

var response = await http.GetStringAsync(uriString);
var rootObject = JsonConvert.DeserializeObject<RootObject>(response);

1 Comment

thanks Sir for your reply,I thought that sponsorises is an array :/
1

Add that to the beginning of each RootObject attribute:

[JsonProperty("fieldName")]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.