2

私はこのフォーラムで多くのバリアントを持つ主題に苦労していますが、私に合ったものを見つけることができないようです.JSON配列が原因だと思います:(私は専門家ではありませんが私はすでに「ほぼ」終わりを迎えています...「成功」と「ステータス」の値を取得する必要がありますが、異なる「アドレス」も取得する必要があります。

私のJSON(responseFromServerと呼ばれます):

{
  "success":true,
  "addresses":
  [
   {"DPID":658584,"SourceDesc":"Postal\\Physical","FullAddress":"1/8 Jonas Street, Waimataitai, Timaru 7910"},
   {"DPID":658585,"SourceDesc":"Postal\\Physical","FullAddress":"2/8 Jonas Street, Waimataitai, Timaru 7910"},
   {"DPID":658583,"SourceDesc":"Postal\\Physical","FullAddress":"3/8 Jonas Street, Waimataitai, Timaru 7910"}
  ],
 "status":"success"
}

次に、このフォーラムの多くの例に基づいて、クラスを作成しました。

public class jsonDataTable
{
    public bool success { get; set; }
    public IEnumerable<dtaddresses> addresses { get; set; }
    public string status { get; set; }
}

public class dtaddresses 
{
    public int DPID { get; set; }
    public string SourceDesc { get; set; }
    public string FullAddress { get; set; }
}

次に、デシリアライズします。

public void _form_OnCallingAction(object sender, ActionEventArgs e)
{
 ...
 ...
 JavaScriptSerializer js = new JavaScriptSerializer();
 jsonDataTable jsonArray = js.Deserialize<jsonDataTable>(responseFromServer);
 ...
 string tb = jsonArray.status.ToString();
 string tb2 = jsonArray.success.ToString();
 ...
 ...
 List<dtaddresses> _listAddresses = new List<dtaddresses>
 {
  new dtaddresses()
 };
 ...
 ...
 try
 {
  string tb3 = _listAddresses.Count.ToString();
  string tb4 = _listAddresses[0].FullAddress;
 }
 catch (Exception ex)
 {
  CurrentContext.Message.Display(ex.Message + ex.StackTrace);
 }
...
...
...
 CurrentContext.Message.Display("Raw Response from server is: {0}", responseFromServer);
 //Returns all the content in a string to check. OK! :)

 CurrentContext.Message.Display("The success value is: {0} ", tb);
 //Returns the Status Value (in this case "success")  OK! :)

 CurrentContext.Message.Display("The status value is: {0} ", tb2);
 //Returns the Success Value (in this case "true")  giggity giggity! All Right! :)

 CurrentContext.Message.Display("The n. of addresses is: {0} ", tb3);
 //Returns how many addresses ( in this case is returning 0) not ok... :(

 CurrentContext.Message.Display("The address value is: {0} ", tb4);
 // Returns the Fulladdress in index 0 (in this case nothing...) not ok... :(

「dtaddresses」クラスの値にアクセスできる理由を理解するのに役立つ人はいますか? これが私が行った距離です...

4

1 に答える 1

3

あなたの質問からコピーした次のコードは、逆シリアル化されたデータとは関係のない新しいリストを作成しています。したがって、常に単一の要素リストになり、最初の要素にはデフォルト値のみが含まれます。これはtb3tb4以降で見られるものです。

List<dtaddresses> _listAddresses = new List<dtaddresses>
{
 new dtaddresses()
};

代わりに、次のようjsonArray.addressesにに割り当てます。_listAddresses

List<dtaddresses> _listAddresses = jsonArray.addresses.ToList() 

または、次のように、完全に忘れて_listAddresses、単にjsonArray.addresses直接参照することもできます。

string tb3 = jsonArray.addresses.Count().ToString();
string tb4 = jsonArray.addresses.First().FullAddress;
于 2013-05-16T04:20:47.977 に答える