-1

I have this JSON string but are not sure how I will parse out the values that are inside:
has
has2

I do succeed to parse out the "id" correctly but are not sure how to access:
CORS
CORS2
CORS3
CORS4

I get the error:
'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.String[]' because the type requires a JSON array (e.g. [1,2,3])

I have pasted the JSON in the pastebin: https://pastebin.com/iWgGV9VK

The code I have:

public void getInfo()
{
    String JSONstring = "{ id: 'hello', name: 'Hello',has:{ CORS: false,CORS2: true},has2:{ CORS3: false,CORS4: true}}"; 
    String id = ""; List<String> has = new List<String>(); List<String> has2 = new List<String>();

    var deserializedTicker = JsonConvert.DeserializeObject<JsonInfo>(JSONstring);
    id = deserializedTicker.id; 
    has = deserializedTicker.has.ToList();
    has2 = deserializedTicker.has.ToList();
}

public class JsonInfo
{
    public String id { get; set; }
    public String[] has { get; set; }
    public String[] has2 { get; set; }
}

I am trying with the dynamic approach using an object but gets an error here also:

''Newtonsoft.Json.Linq.JValue' does not contain a definition for 'id''

//responseBody holds the JSON string
            dynamic stuff = JsonConvert.DeserializeObject(responseBody);
            foreach (var info in stuff)
            {   
                dynamic id = info.Value.id; //''Newtonsoft.Json.Linq.JValue' does not contain a definition for 'id''
                dynamic has = info.Value.has;
                dynamic has2 = info.Value.has2;
                if (has != null && has2 != null)
                {
                    dynamic cors = has.CORS;
                    if(cors != null)
                    {
                        MessageBox.Show(cors.ToString());
                    }
                }
            }
Andreas
  • 1,121
  • 4
  • 17
  • 34
  • 1
    The JSON string both in your code snippet and in your pastebin link isn't valid JSON. – Abion47 Mar 06 '19 at 20:06
  • I don't understand your problem, but the second deserialize(has2) has incorrect. – Leonardo Ramos Duarte Mar 06 '19 at 20:07
  • 1
    Well, look at my comment to your other question: https://stackoverflow.com/questions/55010495/how-to-parse-this-json-string-into-2-liststring from a few hours ago. That comment applies here too. You are here kinda repeating your mistake(s)... –  Mar 06 '19 at 20:07
  • 1
    has and has2 are not strings or arrays of settings, they're complex objects and you'll need to deserislise them as such, or use dynamic or JObject – ADyson Mar 06 '19 at 20:07
  • 1
    I reformatted your code block. Since this is C# (and not javascript) you need to use the icon with the { } symbol on it to format your code, not the one that formats it as javascript. – Richard II Mar 06 '19 at 20:11
  • 1
    Just paste the JSON into your question; there's no reason to make people go to another site. – Heretic Monkey Mar 06 '19 at 20:11
  • I have edit my post with a new approach using dynamic and object but get this error: ''Newtonsoft.Json.Linq.JValue' does not contain a definition for 'id'' – Andreas Mar 06 '19 at 20:31
  • Possible duplicate of [Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List\`1](https://stackoverflow.com/questions/21358493/cannot-deserialize-the-current-json-object-e-g-namevalue-into-type-sy) – Ňɏssa Pøngjǣrdenlarp Mar 06 '19 at 21:19

3 Answers3

2

First off, let's correct your JSON:

{ 
  "id": "hello", 
  "name": "Hello",
  "has": { 
    "CORS": false,
    "CORS2": true
  },
  "has2": { 
    "CORS3": false,
    "CORS4": true
  }
}

Now, the problem you are experiencing is because you are attempting to deserialize the value in "has" and "has2" as arrays. In the JSON, they are not arrays; they are objects. As such, you need to define new classes with the same properties so the JSON can be properly deserialized:

public class JsonInfo 
{
  public string id { get; set; }
  public string name { get; set; }
  public JsonHasInfo has { get; set; }
  public JsonHas2Info has2 { get; set; }
}

public class JsonHasInfo
{
  public bool CORS { get; set; }
  public bool CORS2 { get; set; }
}

public class JsonHas2Info
{
  public bool CORS3 { get; set; }
  public bool CORS4 { get; set; }
}

Now you should be able to deserialize the (correct) JSON properly:

String JSONstring = "{ \"id\": \"hello\", \"name\": \"Hello\", \"has\": { \"CORS\": false, \"CORS2\": true }, \"has2\": { \"CORS3\": false, \"CORS4\": true } }\";"
var deserializedTicker = JsonConvert.DeserializeObject<JsonInfo>(JSONstring);
Abion47
  • 22,211
  • 4
  • 65
  • 88
  • Now I understand. That code works. I try to get a hang of all the scenarios. So it is not an array as I thought it was but an object. So I do follow how you set up the code so nicely. Thank you very much for your help! – Andreas Mar 06 '19 at 20:41
1

You json was incorrect, the key has contains a dict no list.

You need change your deserialize to dictionary or change your json.

Here you can see an example: https://json-schema.org/understanding-json-schema/reference/array.html#array

  • Yes I tried to do an approach using dynamic and object. I did edit my post adding this approach but some error happens there also. – Andreas Mar 06 '19 at 20:33
0

In your JSON, has is an object, not an array. You should model your class to support an object containing the attributes CORS, CORS2, and so on, and so forth.

Edit: If you want to stick to has being an array, you should change your JSON to match what an array expects, which could be like: has: [ false, true ], and omit the CORS thing.

fhcimolin
  • 616
  • 1
  • 8
  • 27