0

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?

1
  • 4
    You are passing Intent as type argument to DeserializeObject where you should pass Intents. Commented Sep 13, 2018 at 1:30

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

Comments

-1

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.

2 Comments

I recommend comparing your answer to the accepted answer to see where you went wrong :-)
John, I didn't go wrong. When I answered this question, Geraldo had mentioned the first issue. I didn't want to repeat whatever he answered. I had found another issue and I mentioned it. I feel that answer edited later by adding score issue.

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.