-2

I have a json in my hand and I want to get a piece of this json in from the c # console app.

I want to use JavaScriptSerializer Deserialize operation but if I give full json in this method I get an error like not valid json. That's why I want to give piece of json to this method.

How can I fixed it?

Full Json :

{

    "ABCFields": {

    },

    "XYZFields": {

    },

    "OPRFields": {
        "MenuEntity": [

            {
                "id": "89899",
                "title": "Main Menu",
                "link": null,
                "test": "test"
            },
            {
                "id": "2323",
                "title": "Main Menu",
                "link": null,
                "test": "test"
            }

        ]
    }

}

I want to reach something like the following ;

 [
  {
    "id": "89899",
    "title": "Main Menu",
    "link": null,
    "test": "test"
  },
  {
    "id": "2323",
    "title": "Main Menu",
    "link": null,
    "test": "test"
  }
]
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Murat Güzel
  • 94
  • 1
  • 12
  • 1
    "I have a json in my hand" hot damn, put that json down before you cut yourself! Show us the deser code you're trying at the moment. – Davesoft Jul 12 '18 at 11:12
  • Possible duplicate of [Read and parse a Json File in C#](https://stackoverflow.com/questions/13297563/read-and-parse-a-json-file-in-c-sharp) – Obay Abd-Algader Jul 12 '18 at 11:15

2 Answers2

2

First you will need to deseralize to the full object represented by your JSON, using the following class structure:

public class ABCFields
{
}

public class XYZFields
{
}

public class MenuEntity
{
    public string id { get; set; }
    public string title { get; set; }
    public object link { get; set; }
    public string test { get; set; }
}

public class OPRFields
{
    public List<MenuEntity> MenuEntity { get; set; }
}

public class RootObject
{
    public ABCFields ABCFields { get; set; }
    public XYZFields XYZFields { get; set; }
    public OPRFields OPRFields { get; set; }
}

This can't be skipped, but as you'll be deserializing the nested object the performance hit of deserializing everything else in the JSON response is negligible and shouldn't cause concern.

When you deserialize you can simply pick out the nested object and disregard everything else in one link:

var oprfields = new JsonSerializer...<FullObjectType>().OprFields;

This deserialization above will depend on what you call your parent class.

James Gould
  • 4,492
  • 2
  • 27
  • 50
-1

It's heretical, and it's vb, but here you go:

Function dirty() As Object()
    Dim s As String = "{""ABCFields"":{},""XYZFields"":{},""OPRFields"":{""MenuEntity"":[{""id"":""89899"",""title"":""MainMenu"",""link"":null,""test"":""test""},{""id"":""2323"",""title"":""MainMenu"",""link"":null,""test"":""test""}]}}"
    Dim js As New System.Web.Script.Serialization.JavaScriptSerializer()
    js.MaxJsonLength = Integer.MaxValue
    Dim o = js.DeserializeObject(s)
    Dim theThingYouWant() As Object = o("OPRFields")("MenuEntity")
    Return theThingYouWant
    For Each d As Dictionary(Of String, Object) In theThingYouWant 'for fun
        MsgBox(d("title"))
    Next
End Function
Davesoft
  • 724
  • 1
  • 4
  • 10