简体   繁体   中英

Converting JSON string to C# object/array

I am looking to convert the following data into ac# object or array in which i can display each item (eventually to be displayed in a list view).

the json itself looks like this:

[
    {
        "commonName": "uni_comp_4",
        "processorID": "BFEFBDEB001201"
    },
    {
        "commonName": "lib_comp_12",
        "processorID": "BFEFBDEB004323"
    }
]

I have looked here for help however I think I may have to take another approach as my system is slightly different.

I'm using a class:

public class API_Response
{
    public bool IsError { get; set; }
    public string ErrorMessage { get; set;
    public dynamic ResponseData { get; set; }
}

for data carrying. My JSON "data" is as shown above however I have been having issues deserialising this.

Initially, I tried:

API_Response r = JsonConvert.DeserializeObject<API_Response>(response);

and I'm able to see the JSON string with MessageBox.show(r.ResponseData). Which inherently is not deserialised.

Additionally, I have tried declaring the following in the same method:

public class RootObject
{
    public string commonName { get; set; }
    public string processorID { get; set; }
}

with no luck in displaying this data individually (or at all).

Essentially, I'm trying to put class API_Response's "ResponseData" into an object and I'm having difficulty.

Shouldn't you deserialize into RootObject[] , rather than API_Response ? Also, depending on the settings you may need to have C# variables in Pascal casing, that is CommonName and ProcessorID

You are trying to deserialize the object with the class which doesn't have elements mentioned in the JSON. Try to put List<RootObject> because your JSON contains the list of the RootObject class that you have created. Try the below solution if it works for you.

JsonConvert.DeserializeObject<List<RootObject>>(response);

just use a decoder to decode the message

Struct PCS: Decodable
{
 let commonName : String?
 let proccesorID : String?

}

var pcs = [PCS]()

put this in viewdidload

func parseData(){

        let jsonUrlString = "Your_API_URL"
        guard let url = URL(string: jsonUrlString) else { return }

        URLSession.shared.dataTask(with: url) {(data, response, err) in

            guard let data = data else { return }

            do {

                let decoder = JSONDecoder()
                pcs = try decoder.decode([Course].self, from: data)

                DispatchQueue.main.async {

                    self.tableView.reloadData()

                }


            } catch let jsonErr{
                print("error", jsonErr)
            }


            }.resume()

in tableView call the data like this

pcs[indexPath.row].commonName
public class RootObject{
   public string commonName { get; set; }
   public string processorID { get; set; }
}

public class API_Response
{
    public bool IsError { get; set; }
    public string ErrorMessage { get; set; }
    public RootObject[] ResponseData { get; set; }
}


API_Response r = JsonConvert.DeserializeObject<API_Response>(response);

I can then use this format for calling the data

MessageBox.Show("" + r.ResponseData[0].commonName);

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