2017-05-23 13 views
0

この音は非常に簡単です。しかし、私はそれを行う方法を見つけることができません。JSON.Netは文字列内のJSON文字列を逆シリアル化します

私はapiから悪いjsonを受け取りました。実際のJSONが文字列

代わりの

[{\"ProductId\":1,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":2,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":3,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000}] 

内にある私が

JsonConvert.DeserializeObject<List<Product>> (jsonString) 

をしようとすると、私はエラーError converting to System.Collections.Generic.List

方法を得る

"[{\"ProductId\":1,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":2,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000},{\"ProductId\":3,\"ProductName\":\"abuka\",\"Rate\":6.00,\"Quantity\":10.000}]" 

を受けていますc私はデシリアライズする前に有効なJSON文字列にそれを抽出しますか?

+0

が*確かに*バックスラッシュはありますか、デバッガでこれを見ていますか? –

+3

いつでもAPIの作成者に戻り、有効な出力を作成するよう依頼することができます。私は、有効なjsonとは何かに関するいくつかのドキュメントがあると確信しています(エスケープする引用符はおそらく有効ではありません)。 APIの出力をデコードしないさまざまなサイトへのリンクを含むあなたの調査をバックアップしてください。 – Neil

+0

@JonSkeetデバッガではありません。これが私の反応を得る方法です。レスポンスの生データからレストクライアント – HeisenBerg

答えて

2

「JSONとしてシリアル化された文字列」という値がある場合は、最初にそれを逆シリアル化します。あなたの文字列本当に開始を仮定し、二重引用符で終わる、あなたはそのアンラップを行うためにJsonConvert.DeserializeObject<string>を呼び出すために罰金する必要があります:

using System; 
using System.IO; 
using Newtonsoft.Json; 

public class Model 
{ 
    public string Foo { get; set; } 
} 

public class Test 
{ 
    static void Main() 
    { 
     string json = "\"{\\\"foo\\\": \\\"bar\\\"}\""; 
     Console.WriteLine($"Original JSON: {json}");   
     string unwrappedJson = JsonConvert.DeserializeObject<string>(json); 
     Console.WriteLine($"Unwrapped JSON: {unwrappedJson}"); 
     Model model = JsonConvert.DeserializeObject<Model>(unwrappedJson); 
     Console.WriteLine($"model.Foo: {model.Foo}"); 
    } 
} 
+0

それは働きます。ありがとうございました – HeisenBerg

1

おかげジョンスキート。あなたの答えを参照すると、アプリケーションで "Newtonsoft.Json"を使用していないが、データを提供するWeb APIである "Newtonsoft.Json"を使用するユーザーは、参考になる可能性があります。

例:WEB APIの

- "Newtonsoft.Jsonは" WEB APIを消費し、使用していない "Newtonsoft.Json"(MVC/ASP.NET Webフォームであるかもしれない)

アプリケーションを使用。したがって、彼らは "System.Web.Extensions"を参照し、using System.Web.Script.Serializationを使用してjsonデータを初期化します。

using System.Collections.Generic; 
using System.Web.Script.Serialization; 

namespace DesrializeJson1ConsoleApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string jsonstring = "\"[{\\\"ProductId\\\":1,\\\"ProductName\\\":\\\"abuka\\\",\\\"Rate\\\":6.00,\\\"Quantity\\\":10.000},{\\\"ProductId\\\":2,\\\"ProductName\\\":\\\"abuka\\\",\\\"Rate\\\":6.00,\\\"Quantity\\\":10.000},{\\\"ProductId\\\":3,\\\"ProductName\\\":\\\"abuka\\\",\\\"Rate\\\":6.00,\\\"Quantity\\\":10.000}]\""; 
      var serializer = new JavaScriptSerializer(); 
      var jsonObject = serializer.Deserialize<string>(jsonstring); 
      List<Product> lstProducts = serializer.Deserialize<List<Product>>(jsonObject); 
      foreach(var item in lstProducts) 
      { 
       Console.WriteLine("ProductId :" + item.ProductId); 
       Console.WriteLine("ProductName :" + item.ProductName); 
       Console.WriteLine("Rate :" + item.Rate); 
       Console.WriteLine("Quantity :" + item.Quantity); 
       Console.WriteLine("--------------------------"); 
      } 
      Console.Read(); 
     } 
    } 
    public class Product 
    { 
     public int ProductId { get; set; } 
     public string ProductName { get; set; } 
     public double Rate { get; set; } 
     public double Quantity { get; set; } 
    } 

} 

OUTPUT

enter image description here

関連する問題