2015-07-21 113 views
2

我有以下嵌套的JSON:如何序列化嵌套的JSON?

{"Command":"helo", 
"parameter" : {"Configured":false, "ApplicationString":"Something", "Hostname":"some", 
"IPAddress":"0.0.0.0", 
"UniqueID":"", 
"Username":"me"}} 

,我需要通過這個字符串作爲JSON對象爲POST調用我的Web服務在C#。任何人都可以幫助我如何做到這一步?

注:我能夠通過簡單的JSON象下面這樣:

var request = (HttpWebRequest)WebRequest.Create("http://localhost:8084"); 
request.ContentType = "text/json"; 
request.Method = "POST"; 

using (var streamWriter = new StreamWriter(request.GetRequestStream())) 
{ 
    string json = new JavaScriptSerializer().Serialize(new 
    { 
     Command = "test", 
     name="pooja" 
    }); 

    streamWriter.Write(json); 
} 

如果我按照同樣的方式來傳遞嵌套JSON象下面這樣:

string json = new JavaScriptSerializer().Serialize(new 
       { 
        Command = "test", 
        parameter = new JavaScriptSerializer().Serialize(new 
        { 
         Command = "test", 
        }), 

       }); 

我得到以下的輸出: { 「Command」:「test」,「parameter」:「{\」Command \「:\」test \「}」}

回答

2

讓我知道,如果你有任何問題。

void Main() 
{ 
    CommandParamater exampleCommand = new CommandParamater 
    { 
     Command = "Foo", 
     Parameter = new Parameter 
     { 
      ApplicationString = "App String Foo", 
      Configured = true, 
      Hostname = "Bar", 
      IPAddress = "8.8.8.8", 
      UniqueID = Guid.NewGuid().ToString(), 
      Username = "FooBar" 
     } 
    }; 

    string uri = "http://localhost:8084"; 
    string data = JsonConvert.SerializeObject(exampleCommand); 

    Html htmlClient = new Html(); 
    htmlClient.Post(uri, data, "application/json"); 
} 

public class Html 
{ 
    public string Post(string uri, string data, string contentType) 
    { 
     byte[] dataBytes = Encoding.UTF8.GetBytes(data); 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
     request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 
     request.ContentType = contentType; 
     request.ContentLength = dataBytes.Length; 

     using (Stream stream = request.GetRequestStream()) 
     { 
      stream.Write(dataBytes, 0, dataBytes.Length); 
     } 

     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
     using (Stream stream = response.GetResponseStream()) 
     using (StreamReader reader = new StreamReader(stream)) 
     { 
      return reader.ReadToEnd(); 
     } 
    } 
} 


public class Parameter 
{ 
    [JsonProperty("Configured")] 
    public bool Configured { get; set; } 

    [JsonProperty("ApplicationString")] 
    public string ApplicationString { get; set; } 

    [JsonProperty("Hostname")] 
    public string Hostname { get; set; } 

    [JsonProperty("IPAddress")] 
    public string IPAddress { get; set; } 

    [JsonProperty("UniqueID")] 
    public string UniqueID { get; set; } 

    [JsonProperty("Username")] 
    public string Username { get; set; } 
} 

public class CommandParamater 
{ 
    [JsonProperty("Command")] 
    public string Command { get; set; } 

    [JsonProperty("parameter")] 
    public Parameter Parameter { get; set; } 
} 
+1

這就是你想要的。總是創建類來描述你的數據,並使用'JsonConvert'來序列化它。 –

+0

Thanks @Aydin Adn :-) –

+0

我使用了JavaScriptSerializer()。Serialize,如其他示例中所示,並且它沒有使用JsonProperty屬性。使用JsonConvert修復了這個問題。 –

2

它可以幫助您。

private string MakeRequest(string uri, string jsnPostData, string method) 
{ 
    try 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
     if (request != null) 
     { 
      request.Method = method; 
      request.Timeout = 2000000; 
      request.ContentType = "application/json"; 
      request.KeepAlive = true; 

      byte[] data = Encoding.UTF8.GetBytes(jsnPostData); 

      Stream dataStream = request.GetRequestStream(); 
      dataStream.Write(data, 0, data.Length); 
      dataStream.Close(); 

      HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
      string result = new StreamReader(response.GetResponseStream()).ReadToEnd(); 

      return result; 
     } 
     else 
      return ""; 
    } 
    catch (Exception ex) 
    { 
     Response.Write("<b>Error :</b>" + ex.Message + "</br>"); 
     return ""; 
    } 
} 
+0

請標記爲答案,如果它爲你工作 –