Convert JSON to a C# Array

Convert json to a C# array?

just take the string and use the JavaScriptSerializer to deserialize it into a native object. For example, having this json:

string json = "[{Name:'John Simith',Age:35},{Name:'Pablo Perez',Age:34}]"; 

You'd need to create a C# class called, for example, Person defined as so:

public class Person
{
public int Age {get;set;}
public string Name {get;set;}
}

You can now deserialize the JSON string into an array of Person by doing:

JavaScriptSerializer js = new JavaScriptSerializer();
Person [] persons = js.Deserialize<Person[]>(json);

Here's a link to JavaScriptSerializer documentation.

Note: my code above was not tested but that's the idea Tested it. Unless you are doing something "exotic", you should be fine using the JavascriptSerializer.

How to Convert Json Object to Array in C#

The property names won't match as is, because you subjects class don't have the 'subject_' prefix the JSON object has. Easiest fix is to change your property names as shown in Ali's answer. Just in case you need to keep your property names as is, you can also use a JsonProperty attribute to alter the serialization names (perhaps there's a more generic way using some sort of converter, but didn't think the amount of properties needed it)

    class subjects
{
[JsonProperty("subject_id")]
public int id { get; set; }
[JsonProperty("subject_name")]
public string name { get; set; }
[JsonProperty("subject_class")]
public int class_name { get; set; }
[JsonProperty("subject_year")]
public string year { get; set; }
[JsonProperty("subject_code")]
public string code { get; set; }
}

If you never need the root subjects you can also skip it without dynamic or an extra class with something like:

subjects[] arr = JObject.Parse(result)["subjects"].ToObject<subjects[]>();

(JObject is part of the namespace Newtonsoft.Json.Linq )

What is best way to convert json string into array of objects?

I recommend to install Newtonsoft.Json via the NuGet package manager. Then decorate the members of the POCO class with the JsonProperty attribute to tell the serializer which property matches to a specific JSON key:

public class MyJsonObject
{
[JsonProperty("Value")]
string Value { get; set; }

[JsonProperty("Name")]
string Name { get; set; }
}

You can the deserialize the JSON to a corresponding object instance:

var jsonString = "[{\"Value\": \"1\", \"Name\": \"One\"}, {\"Value\": \"2\", \"Name\": \"Two\"}]";
List<MyJsonObject> myJsonObjects = JsonConvert.DeserializeObject<List<MyJsonObject>>(jsonString);

Converting JSON to C# Array

You are almost there but a small mistake. What you supplied is as the following. Please take notice that there are no commas at the seperation.

{JSONSTUFF}
{JSONSTUFF}
{JSONSTUFF}

But the following says that you want it to be converted to List<address> but it is not a list :(

JsonConvert.DeserializeObject<List<adress>>(json_adress);

So in fact what you need to supply is a list of address,

[
{JSONSTUFF},
{JSONSTUFF},
{JSONSTUFF},
]

Please do not forget the commas :)

[{"name":"test1","email":["test1@test.de"],"imprint":"testimprint1 testimprint1","info2":"testinfo1"},
{"name":"test2","email":["test2@test.de"],"imprint":"testimprint2 testimprint2","info2":"testinfo2"},
{"name":"test3","email":["test3@test.de"],"imprint":"testimprint3 testimprint3","info2":"testinfo3"}]

How to convert JSON ARRAY from IRESTRESPONSE to C# array

For your specific JSON string, you may use the Model classes provided in the solution and also the deserialization process is being done:

A working demo is here: https://dotnetfiddle.net/2VsyYL

using System;
using Newtonsoft.Json;
using System.Collections.Generic;

public class Program
{
public static void Main()
{
string json=@"{'Results':{'output1':[{'Class':'10','age':'9.06725552044283','menopause':'18.5605772106407','tumor-size':'5.62041822714077','inv-nodes':'2.50477840015836','node-caps':'5.29640554298695','deg-malig':'1.77286724180295','breast':'2.67776376618299','breast-quad':'5.61570098311485','irradiat':'4.84969010222608','Scored Labels':'1','Scored Probabilities':'0.999995118824755'}]}}";
var Sresponse = JsonConvert.DeserializeObject<RootObject>(json);

foreach(var result in Sresponse.Results.output1)
{
Console.WriteLine(result.Class);
Console.WriteLine(result.age);
Console.WriteLine(result.deg_malig);
Console.WriteLine(result.Scored_Labels);

}
}
}

public class Results
{
public List<Output1> output1 { get; set; }
}

public class RootObject
{
public Results Results { get; set; }
}

public class Output1
{
public string Class { get; set; }
public string age { get; set; }
public string menopause { get; set; }

[JsonProperty("tumor-size")]
public string tumor_size { get; set; }

[JsonProperty("inv-nodes")]
public string inv_nodes { get; set; }

[JsonProperty("node-caps")]
public string node_caps { get; set; }

[JsonProperty("deg-malig")]
public string deg_malig { get; set; }

public string breast { get; set; }

[JsonProperty("breast-quad")]
public string breast_quad { get; set; }

public string irradiat { get; set; }

[JsonProperty("Scored Labels")]
public string Scored_Labels { get; set; }

[JsonProperty("Scored Probabilities")]
public string Scored_Probabilities { get; set; }
}

This will output:

10
9.06725552044283
1.77286724180295
1


Related Topics



Leave a reply



Submit