-2

i am getting measurements from withings and want to show them in graphs but not able to convert it to json. i also try JsonConvert.SerializeObject(myString) using Newtonsoft.dlland simple

System.Web.Script.Serialization.JavaScriptSerializer sr = new System.Web.Script.Serialization.JavaScriptSerializer();
sr.Serialize(myString);

but it is not converting.

My withings measurement string is as follows.

{
  "status": 0,
  "body": {
    "updatetime": 1392764547,
    "measuregrps": [
      {
        "grpid": 17945868,
        "attrib": 0,
        "date": 139984270,
        "category": 1,
        "measures": [
          {
            "value": 72,
            "type": 9,
            "unit": 0
          },
          {
            "value": 152,
            "type": 10,
            "unit": 7
          },
          {
            "value": 87,
            "type": 17,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 176587495,
        "attrib": 0,
        "date": 13915689,
        "category": 1,
        "measures": [
          {
            "value": 94,
            "type": 9,
            "unit": 0
          },
          {
            "value": 145,
            "type": 10,
            "unit": 0
          },
          {
            "value": 109,
            "type": 11,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 179262494,
        "attrib": 0,
        "date": 1391369607,
        "category": 1,
        "measures": [
          {
            "value": 77,
            "type": 9,
            "unit": 0
          },
          {
            "value": 121,
            "type": 10,
            "unit": 0
          },
          {
            "value": 87,
            "type": 11,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 179258492,
        "attrib": 0,
        "date": 1391171167,
        "category": 1,
        "measures": [
          {
            "value": 61,
            "type": 9,
            "unit": 0
          },
          {
            "value": 107,
            "type": 10,
            "unit": 0
          },
          {
            "value": 80,
            "type": 11,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 179089150,
        "attrib": 0,
        "date": 1391167537,
        "category": 1,
        "measures": [
          {
            "value": 69,
            "type": 9,
            "unit": 0
          },
          {
            "value": 112,
            "type": 10,
            "unit": 0
          },
          {
            "value": 67,
            "type": 11,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 179079661,
        "attrib": 2,
        "date": 1391164672,
        "category": 1,
        "measures": [
          {
            "value": 720,
            "type": 1,
            "unit": -1
          }
        ]
      },
      {
        "grpid": 17998560,
        "attrib": 2,
        "date": 146989672,
        "category": 1,
        "measures": [
          {
            "value": 284,
            "type": 4,
            "unit": -2
          }
        ]
      }
    ]
  }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Aqib Shehzad
  • 79
  • 1
  • 8
  • What happens? Do you get an error message? have you tried a smaller/subset of your JSON? – n8wrl Feb 18 '14 at 18:26
  • no i didn't get error but the same output as input – Aqib Shehzad Feb 18 '14 at 18:29
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 18 '14 at 19:19

2 Answers2

1

It seems, you want to deserialize your json string, not serialize:

var obj = JsonConvert.DeserializeObject<Withings.RootObject>(json);

public class Withings
{
    public class Measure
    {
        public int value { get; set; }
        public int type { get; set; }
        public int unit { get; set; }
    }

    public class Measuregrp
    {
        public int grpid { get; set; }
        public int attrib { get; set; }
        public int date { get; set; }
        public int category { get; set; }
        public List<Measure> measures { get; set; }
    }

    public class Body
    {
        public int updatetime { get; set; }
        public List<Measuregrp> measuregrps { get; set; }
    }

    public class RootObject
    {
        public int status { get; set; }
        public Body body { get; set; }
    }
}
L.B
  • 114,136
  • 19
  • 178
  • 224
0

JsonConvert.SerializeObject(myString) takes an object an returns a string. If you want to turn a string into an object you want to use Deserialize<T>(sting json). Given the arguments name in your sample is myString I would assume you're using the method wrong.

To deserialize you need an equivalent type like;

   public class myObject
   {
        public int status { get; set; }
        public Body body { get; set; } 
   } 
   public class Body
   {
            //other parameters ect
    }

Your object model needs to exactly match the json in order by Deserialize<T> to behave correctly.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115