0

I am looking to convert the following data into a c# 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.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Cragie
  • 17
  • 6
  • 6
    JsonConvert.DeserializeObject>(response); – Sir Rufo Nov 19 '18 at 01:43
  • im using API_Response to get the data, i think i maybe have to put the RootObject inside the API_Response Class? so that i can separate the response into CommonName and Processor ? – Cragie Nov 19 '18 at 01:50

4 Answers4

0

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

Prix
  • 19,417
  • 15
  • 73
  • 132
Felix
  • 9,248
  • 10
  • 57
  • 89
  • API_Response's "ResponseData" is what i think i need to deserialise, RootObject isnt linked in any form at the moment – Cragie Nov 19 '18 at 01:51
  • 2
    @Cragie Computers are totally dumb. They will follow your code and not your thougths. If there is something related to RootObject you have to code that – Sir Rufo Nov 19 '18 at 01:59
0

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);
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Am i supposed to run this command in order to make the alteration or must I set this to a variable? I set it to a "dynamic" variable and then tried to print it however i received this error: "{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[infosecQuiz.authorisationMenu+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive ty... – Cragie Nov 19 '18 at 18:58
-1

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
-1
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);
Cragie
  • 17
  • 6
  • 1
    Nope, this will only result in an exception [.Net Fiddle example](https://dotnetfiddle.net/gkwjrc) ... or you present us the **wrong** json response string in your question - [this example](https://dotnetfiddle.net/3AqPNa) works – Sir Rufo Nov 20 '18 at 08:00