0

I have a json which a form like this:

[
     {
           "sample1" : "example",
           "json": "{\"number\":1,\"Files\":\"[{\\\"cols\\\": [\\r\\n {\\r\\n \\\"name\\\": \\\"A\\\",\\r\\n \\\"key\\\": 0\\r\\n },{\\r\\n \\\"name\\\": \\\"B\\\",\\r\\n \\\"key\\\": \\r\\n },{\\r\\n \\\"rows\\\": [\\r\\n [\\r\\n \\\"Example\\\",\\r\\n \\\"1234abcd\\\\r\\\\n(sample)\\\"\\r\\n ],\\r\\n [\\r\\n \\\"example1 \\\",\\r\\n \\\"17 18\\\"\\r\\n ]\\r\\n ]\\r\\n }\\r\\n ]\"}"
    }
  ]

And I want to show like this:

[
{
"sample1": "example",
"json": {
  "number":1,
  "Files":{
  "cols":[
{
  "name":"A",
  "key":0
},
{
  "name":"B",
  "key":1
}
],
   "rows":
[
[
"Example","1234abcd(sample)"
],
[
"example1","17 18"
] 
]
}
}
}
]

However, my code can't parse the first one into the second one. It throw exception NullReferenceException. So how can I resolve the problem?

Hai Nguyen
  • 13
  • 7
  • I think the solution is similar to this [Solution JSON-Property as JSON](https://stackoverflow.com/questions/17396703/jsonproperty-json-object-inside-json-object) – codlix Jul 27 '19 at 20:48
  • 2
    You need to show your code that caused the exception. – Gabriel Luci Jul 27 '19 at 20:54
  • Do you have a class that you're deserializing into? Can you show the JSON deserialization code that you're using along with the class definitions? Cheers – joelc Jul 28 '19 at 00:03
  • The cause the simple command: db.Samples – Hai Nguyen Jul 28 '19 at 08:43
  • Hi Joelc, yes i do. First I call the entry that has the data in the entity. Then, I use JsonConvert to Deserialize the object – Hai Nguyen Jul 28 '19 at 11:22

1 Answers1

0

its not a real answer, but, 1. you have a triple nested json 2. if you run my example , you will see you have issue in last trapped json (files):

[{"cols": [
{
"name": "A",
"key": 0
},{
"name": "B",
"key": 
},{
"rows": [
[
"Example",
"1234abcd\r\n(sample)"
],
[
"example1 ",
"17 18"
]
]
}
]

i used your json in request to create output on each step:

string j1 = File.ReadAllText("c:\\1\\json.txt");
    var r = JsonConvert.DeserializeObject<List<parentJson>>(j1);
    var obj = JsonConvert.DeserializeObject<jparced>(r.FirstOrDefault().json);
    Console.Write(obj.files);

and classes:

public class parentJson{
    public string sample1 {get;set;}
    public string json {get;set;}

    public parentJson(){}
}

public class jparced
{
    public string number { get; set; }
    public string files {get;set;}

}


EDITED: i modified the last JSON part to be valid. please compare it to the data you have. one filed does not have a value, and formatting is off.

[
    {
        "cols": [
            {
                "name": "A",
                "key": 0
            },
            {
                "name": "B",
                "key": "*****NULL VALUE******"
            }
        ],
        "rows": [
            [
                "Example",
                "1234abcd\r\n(sample)"
            ],
            [
                "example1 ",
                "17 18"
            ]
        ]
    }
]
Power Mouse
  • 727
  • 6
  • 16