简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM