【问题标题】:Read nested JSON object读取嵌套的 JSON 对象
【发布时间】:2016-04-20 09:27:33
【问题描述】:

我想读取嵌套的 JSON 并将数据存储在数据库中。我将 JSON 对象作为“application/json”从 post 方法获取到我的 WCF 服务。

为此,我将 JSON 转换为字典。但仅获得第一级值。获取二级 JSON 的对象。

下面是我用来将 JSON 转换为字典的代码:

[Serializable]
public class JsonDictionary : ISerializable
{
    private Dictionary<string, object> m_entries;

    public JsonDictionary()
    {
        m_entries = new Dictionary<string, object>();
    }

    public IEnumerable<KeyValuePair<string, object>> Entries
    {
        get { return m_entries; }
    }

    protected JsonDictionary(SerializationInfo info, StreamingContext context)
    {
        m_entries = new Dictionary<string, object>();
        foreach (var entry in info)
        {
            m_entries.Add(entry.Name, entry.Value);
        }
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (var entry in m_entries)
        {
            info.AddValue(entry.Key, entry.Value);
        }
    }
}

以下是向 WCF 发布的 JSON:

在此,获取对象而不是“office_address”、“financial_details”、“residence_address”和“personal_details”的值。

{
    "business_name": "Test Business",
    "fk_loan_id": "kh-aaaaa3232",
    "proprietor_details": {
        "office_address": {
            "email_id": "aaa.aaa@aaa.com",
            "alternative_mobile_number": "00000",
            "pincode": "00000",
            "landline_number": "00000",
            "city": "Tutne",
            "flat_number": "000",
            "street": "Test Street",
            "locality": "tLocality",
            "state": "SGDS",
            "mobile_number": "0000000000"
        },
        "date_of_incorporation": "01/03/2010",
        "financial_details": {
            "TAN": "ghdg45341f",
            "TIN": "dc5345fg6g",
            "VAT": "dgdgd544t4",
            "PAN": "AAAAA7614A"
        },
        "residence_address": {
            "email_id": "aaa.-aa@aa.com",
            "alternative_mobile_number": "0000000",
            "pincode": "00000",
            "landline_number": "00000",
            "city": "SFSS",
            "flat_number": "055",
            "street": "DASAW",
            "locality": "Local",
            "state": "AAAAA",
            "mobile_number": "000000"
        },
        "personal_details": {
            "gender": "M",
            "date_of_birth": "06/07/1969",
            "last_name": "AAA",
            "middle_name": "A",
            "first_name": "AAAA"
        }
    }
}

【问题讨论】:

  • 您已将您的字典定义为Dictionary&lt;string, object&gt;,并且您将获得一个object 作为密钥。这就是Dictionary&lt;string, object&gt; 的作用。如果您希望它返回对象以外的其他内容,请不要使用 object 作为您的值类型。
  • 我使用的任何东西,都将 office_address 视为计数为 0 的对象

标签: c# json wcf


【解决方案1】:

使用这个 - var innerProperties = yourObject.proprietor_details;

这将为您提供 proprietor_details 中的属性。

我创建了一个 mvc web api 这是代码

namespace TestWebApi.Controllers
{
public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/values
    public void Post(ComplexClass data)
    {
        var xx = data.business_name;
        var y = xx + data.date_of_incorporation;
    }

    // PUT api/values/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/values/5
    public void Delete(int id)
    {
    }
}

}

public class ComplexClass
{
    public string business_name { get; set; }
    public string fk_loan_id { get; set; }
    public proprietor_details proprietor_details {get;set;}
    public string date_of_incorporation { get; set; }
    public financial_details financial_details { get; set; }
    public residence_address residence_address {get;set;}
    public personal_details personal_details {get;set;}
}

public  class financial_details
{
  public string TAN { get; set; }
  public string TIN { get; set; }
  public string VAT { get; set; }
  public string  PAN { get; set; }
  }

让其他类也...

这是我正在使用的 post call 的 json 数据 { "business_name": "测试业务", “fk_loan_id”:“kh-aaaaa3232”, “所有者详细信息”:空, "date_of_incorporation": "01/03/2010", “财务细节”:{ “谭”:“ghdg45341f”, “锡”:“dc5345fg6g”, "增值税": "dgdgd544t4", “潘”:“AAAAA7614A” }, “居住地址”:空, “个人详细信息”:空 } 为提琴手调用附加图像

附加数据树图像 [

【讨论】:

  • 嗨,我正在使用以下方法来读取创建的字典: public Main ApplyNewLoan(ApplyNewLoan applyloan) { var innerProperties = applyloan.proprietor_details;返回 db.ApplyLoan(applyloan);但是有了这个我可以阅读到 proprietor_details,作为 jSonDictionary。我还需要阅读“office_address”。但它把它当作一个对象来阅读。
  • 那你应该试试你的Object.proprietor_details.office_address
  • 我为相同的测试创建了一个 api。我从我的 web api 中的 json 数据中获得了适当的属性。
  • 嗨,m 又卡在 json 字符串中了。
  • "loan_application_documents": {"entity_documents_details_list": [ {"entity_type": "test entity", "documents": [{ "metadata_list": [{ "document_name": "test1 doc", "文件扩展名:“.doc”,“document_url”:“www.google.com”}],“document_id”:“doc123”,“document_type”:“测试类型”}],“entity_id”:“ent123”}]我可以读取所有值,除了“document_name”、“file_extension”、“document_url”。将 null 作为这些值。请帮忙。
猜你喜欢
  • 1970-01-01
  • 2020-11-09
  • 2022-11-21
  • 2018-10-12
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
相关资源
最近更新 更多