Brandon Wang
Brandon Wang

Reputation: 13

how to deserialize a custom json to c# object

I use Newtonsoft.json to deserialize this json

{
    "pdf_info": [
        [
         -> this is a object {
           "order_serial_no": "xxxxx",
           // more properties
         },
        -> this is an array ["xxxx", "x"]
        ]
    ]
}

In java I can use the following code to achieve this.

JSONArray pdfArray = JSONArray.parseArray(pdf_info);
String pdfArrayOne = pdfArray.getString(0);
JSONArray jsonArray = JSONObject.parseObject(pdfArrayOne, JSONArray.class);
String jsonData = jsonArray.getString(0);
Pdf pdf = JSONObject.parseObject(jsonData, Pdf.class);

So, how to deserialize this json with newtonsoft.json

Upvotes: 0

Views: 85

Answers (2)

dba
dba

Reputation: 1175

Apparently (after removing your comments) this would be the c# class of your object (Copy the json to clipboard -> in Visual Studio 'Edit->paste special->paste json as class' - see more on this)

public class Rootobject
{
  public object[][] pdf_info { get; set; }
}

Define this Type and you would be able to deserialize it using:

Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(System.IO.File.ReadAllText(fileName));

Upvotes: 1

Awaitable
Awaitable

Reputation: 1

Firstly -> read the docs.
Secondly:
JObject data = JObject.Parse(jsonText); // deserialize to dict-like object

Upvotes: 0

Related Questions