raspi surya
raspi surya

Reputation: 129

JSON to array in C#

I have a program that produce a JSON format. What I want to do is to store the json result into array in C#.

the json receive from API:

var strResponseContent = await response.Content.ReadAsStringAsync();
Result.Text = strResponseContent.ToString(); **<-- this is working fine**

here is the look of json:

{
    "query": "banana",
    "topScoringIntent": {
        "intent": "banana",
        "score": 0.9086001
    },
    "intents": [{
            "intent": "banana",
            "score": 0.9086001
        }, {
            "intent": "bananania",
            "score": 0.559515059
        }
    ]
}

and to store the json into array. here is the structure:

public class Intents
    {
        public List<Intent> intents { get; set; }
    }

    public class Intent
    {
        public string intent { get; set; }
        public int score { get; set; }
    }

and finally, to convert, I use the deserialize object

Intents intents = JsonConvert.DeserializeObject<Intent>(strResponseContent);

however, during the store into json, the error comes like "can't implicitly convert type Intent to Intents"

what Is my mistake? how to correct it?

Upvotes: 0

Views: 128

Answers (2)

Nick Mehrdad Babaki
Nick Mehrdad Babaki

Reputation: 12555

You defined the score as integer

 public int score { get; set; }

but score is not integer. changing it to double or decimal will fix it.

Upvotes: -1

Geraldo Diaz
Geraldo Diaz

Reputation: 356

there are two things first score is not a valid int... so, try changing int for Decimal. And second try doing this:

Intents intents = JsonConvert.DeserializeObject<Intents>(strResponseContent);

Upvotes: 4

Related Questions