0

Its been a day scratching my head & googling on how to convert json string to an object.

This is my json.

   {
"statusCode": 200,
"data": {
"items": [
  {
    "id": 2623,
    "JsonData": "{\"Number\":143,\"IsDeleted\":false,\"GapAnalysisChecked\":false,\"ShowGraphics\":true,\"Impact\":{\"Value\":\"DefaultNodeTitle_Impact\",\"Details\":null,\"DefaultValue\":\"DefaultNodeTitle_Impact\",\"Id\":\"0a507b25-cf0f-4ee3-8262-76b29adbda4e\"},\"Gap\":{\"Value\":\"DefaultNodeTitle_Gap\",\"Details\":null,\"DefaultValue\":\"DefaultNodeTitle_Gap\",\"Id\":\"cd411a47-a215-4dda-a8af-19686ad0d090\"}"}]
}

}

I am in need to convert JsonData to a single object.

var obj = JsonConvert.DeserializeObject(res.JsonData);
res.JsonData = obj; //this is giving error.- Cannot convert implicilty string to an object

Along with it, I have tried with number of permutatiation like DeserializeObject<Customer>() & all such things.

My desired output.

{
"statusCode": 200,
"data": {
"items": [
  {
    "id": 2623,
    "JsonData": object
}

MY DTO

public class Res
{
  public string StatusCode {get;set;}

  public string JsonData {get; set;}
}

Any help/suggestion highly appreciated.
Thanks.

7
  • //this is giving error. isn't descriptive.. What error? Commented Feb 20, 2017 at 14:13
  • @ColinM, addded Commented Feb 20, 2017 at 14:14
  • Without seeing your code, res.JsonData = (object)obj; ? Commented Feb 20, 2017 at 14:15
  • { "statusCode": 200, "data": { "items": [ { "id": 2623, "JsonData": object }]} this is valid JSON Commented Feb 20, 2017 at 14:16
  • @MANISHKUMARCHOUDHARY, sorry!! I didn't get you Commented Feb 20, 2017 at 14:17

2 Answers 2

9

You're missing "]"

EDIT

One possible implementation would be something like this :

Your classes

public class Res{

   public string statusCode {get;set;}
   public Data data {get; set;}


}


 public class Data{

  public List<Item> items {get; set;}

  }


 public class Item{

    public string id {get; set;}
    public JsonData JsonData {get;set;}

}

 public class JsonData{

    public string name {get; set;}
    public string from {get; set;}
 }

Your json

{
"statusCode":200,
"data" :
     {
       "items" :
           [
             {
              "id":2623,
              "JsonData" :{
               "name":"joab",
               "from":"Brazil"}
               },
               {
               "id":2624,
               "JsonData": {
               "name":"mary",
               "from":"USA"
               }
             }
          ]
     }
}

var res = new JavaScriptSerializer().Deserialize<Res>(yourjson);
Sign up to request clarification or add additional context in comments.

2 Comments

Corrected the json string
added the class
0

1 - Add new class
2 - Copy your json
3 - Visual Studio Menu > Edit > Paste Special > Paste JSON as Classes

See

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.