0
string json_index = '"libraries": [
    {
        "name": "test1",
        "natives": {
            "windows": "natives-windows"
        },
        "downloads": {
            "classifiers": {
                "natives-windows": {
                    "url": "http://test1.com/"
                }
            }
        }
    },
    {
        "name": "test2",
        "natives": {
            "windows": "natives-windows"
        },
        "downloads": {
            "classifiers": {
                "natives-windows": {
                    "url": "http://test2.com/"
                }
            }
        }
    }
]';
dynamic jsonObj = JsonConvert.DeserializeObject(json_index);
foreach (var obj in jsonObj.libraries)
{
    label1.Text += "\n" + obj.downloads.classifiers.natives-windows.url; // error here
}

Can not detect the "-" sign between words.

I actually thought that:

string nativeswindows = obj.natives.windows;
label1.Text += "\n" + obj.downloads.classifiers.nativeswindows.url;

but it did not work

How can I get the "url" in "natives-windows" ?

I am using Newtonsoft JSON.

Oguzhan
  • 3
  • 2
  • 2
    Possible duplicate of [How to deserialize a property with a dash (“-”) in it's name with NewtonSoft JsonConvert?](https://stackoverflow.com/questions/14753113/how-to-deserialize-a-property-with-a-dash-in-its-name-with-newtonsoft-jso) – Paul-Jan Aug 07 '18 at 10:03
  • Possible duplicate of [Can you have a property name containing a dash](https://stackoverflow.com/questions/5771577/can-you-have-a-property-name-containing-a-dash) – Smartis has left SO again Aug 07 '18 at 10:05

2 Answers2

0

So there's a few steps to this.

First you need to define a concrete class to represent your JSON. I've done this using http://json2csharp.com, with the output being here:

public class Natives
{
    public string windows { get; set; }
}

public class NativesWindows
{
    public string url { get; set; }
}

public class Classifiers
{
    public NativesWindows __invalid_name__natives-windows { get; set; }
}

public class Downloads
{
    public Classifiers classifiers { get; set; }
}

public class Library
{
    public string name { get; set; }
    public Natives natives { get; set; }
    public Downloads downloads { get; set; }
}

public class RootObject
{
    public List<Library> libraries { get; set; }
}

Your problematic field has been flagged up by this tool too, seen here:

public NativesWindows __invalid_name__natives-windows { get; set; }

So we need a way to assign the JSON Key/Value pair to a valid C# field. We can does this using Attributes.

For this field in particular, we can use the JsonProperty attribute to take in the JSON property name and assign it to a C# field on your new concrete class. This looks like:

[JsonProperty("native-windows")]
public NativesWindows NativeWindowsObj { get; set; }

You can put that into your new concrete class, and then use the following to deserialize to that type:

Natives jsonObj = JsonConvert.DeserializeObject<Natives>(json_index);

This is telling Newtonsoft:

  1. I have a property name native-windows.
  2. I'm deserializing my JSON to this specific class.
  3. The invalid C# identified native-windows matches a JsonProperty I've specified in my class, assign the value to that matching attribute.
  4. Return the full, deserialized object.
James Gould
  • 4,492
  • 2
  • 27
  • 50
0

you try:

  label1.Text += "\n" + obj.downloads.classifiers["natives-windows"].url;

I found this link: Parsing JSON w/ @ at sign symbol in it (arobase)

Hope it will help you!

Tran Audi
  • 587
  • 1
  • 6
  • 22