JSON cannot be deserialized for an object, do you need an array?

I am trying to take the incoming JSON elements and associate them with the list elements, but the visual studio told me that I need to make an array, not an object? I never had to do this ... Does anyone know how?

My RootObject:

public class RootObject
{
    public string url { get; set; }
    public string display { get; set; }
    public List<string> genetics { get; set; }
    public List<string> price { get; set; }
    public List<string> brandMaker { get; set; }
    public string form { get; set; }
    public string dosornos { get; set; }
    public string qty { get; set; }
    public string mfg { get; set; }
    public string mobURI { get; set; }
}

Note. Genetics, price, BrandMaker actually return nothing but a value, as shown below:

"genetics": [
    "typeophere"
],
"price": [
    "$1400"
],

JSON FILE / REQUEST MAIN RESULTS:

  [
{
    "url": "N/A",
    "display": "",
    "genetics": [
        "microogiz"
    ],
    "price": [
        "96.016"
    ],
    "brandMaker": [
        "Oshi Kunti Multikashi, Osaka, JP"
    ],
    "form": "tangent",
    "dosornos": "n/a",
    "qty": "88G",
    "mfg": "SelfMade Industries, USA Dist.",
    "mobURI": "n/a"
}

]

My source code:

// Get JSON via WEB
string ProviderURI = goURI;
webClient webClient = new WebClient();
webClient.DownloadStringCompleted += new  
    DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(ProviderURI));

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        return;
    }

    var deserializedJSON = JsonConvert.DeserializeObject<RootObject>(e.Result);
    lstBoxResults.ItemsSource = deserializedJSON; // or deserializedJSON.url.ToString();
}
+2
source share
2 answers

, , , JSON . RootObject - .

JSON. , .

{
    "url": "http://www.stackoverflow.com",
    "display": "This is a test",
    "genetics": [
        "typeophere"
    ],
    "price": [
        "$1400"
    ],
    "form": "a form"
}

RootObject. Pascal JSON.NET, /.

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class RootObject
{
    [JsonProperty(PropertyName = "url")]
    public string Url { get; set; }

    [JsonProperty(PropertyName = "display")]
    public string Display { get; set; }

    [JsonProperty(PropertyName = "genetics")]
    public List<string> Genetics { get; set; }

    [JsonProperty(PropertyName = "price")]
    public List<string> Price { get; set; }

    [JsonProperty(PropertyName = "form")]
    public string Form { get; set; }
}

-, , JSON . .

string json;
var resource = Application.GetResourceStream(new Uri("json.txt", UriKind.Relative));
using (var reader = new StreamReader(resource.Stream))
{
    json = reader.ReadToEnd();
}

WebClient, JSON.

JSON RootObject.

var rootObject = JsonConvert.DeserializeObject<RootObject>(json);

. ListBox. ItemSource ListBox, , , . , rootObject . .

lstBoxResults.ItemsSource = rootObject;

. RootObject System.Collections.IEnumerable. , ItemsSource.

. .

lstBoxResults.ItemsSource = new List<RootObject> { rootObject };

, JSON rootObject. ListBox .

, JSON RootObjects. , , "", RootItems.

{
    "items": [
    {
    "url": "http://www.stackoverflow.com",
    "display": "This is a test",
    "genetics": [ "typeophere" ],
    "price": [ "$1400" ],
    "form": "a form"        
    },
    {
    "url": "http://cgeers.com",
    "display": "This is another test",
    "genetics": [ "typeophere" ],
    "price": [ "$2000" ],
    "form": "another form"
    }]
}

Deserialing . JSON.NET RootObjects.

var data = JObject.Parse(json)["items"];

(IEnumerable).

var rootObjects = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(data.ToString());

RootObject, . ListBox.

lstBoxResults.ItemsSource = rootObjects;

, JSON, , . , , JSON.

:

json = json.Substring(json.IndexOf("[") + 1);
json = json.Substring(0, json.LastIndexOf("]"));
var rootObjects = JsonConvert.DeserializeObject<RootObject>(json);
+12

RootObject rootObject;

if (json.startsWith("["))
{
  rootObject = JsonConvert.DeserializeObject<List<RootObject>>(json)[0];
}
else
{
  rootObject = JsonConvert.DeserializeObject<RootObject>(json);
}
+6

All Articles