需要一种从JSON字符串获取JObject的有效方法

时间:2014-10-30 21:19:10

标签: c# json json.net

"[{\"Key\":\"Response\",\"Value\":[{\"OrderID\":78749,\"OrderItemID\":335451,\"ProgramID\":195903,\"ProgramName\":\"UNIVERSITY\",\"ProgramDescription\":\"UNIVERSITY\",\"ScheduleDate\":\"2013-09-18T00:00:00\",\"SubmitOrderRequestDate\":\"2013-07-21T19:05:00\",\"OrderTypeName\":\"AlaCarte\",\"OrderTypeDescription\":\"AlaCarte\",\"ProductID\":null,\"ProductName\":null,\"ProductDescription\":null,\"ProductHTMLTitle\":null,\"EditionName\":\"Spring 2012-D\",\"EditionID\":78814,\"EditionDescription\":\"multi\",\"SpecialInstruction\":null,\"OrderDeliveryTypeName\":\"W\",\"OrderDeliveryTypeDescription\":\"server (downloaded)\",\"OrderItemStatusName\":\"Needs to be Compared\",\"OrderItemStatusDescription\":\"Needs to be Compared\",\"EditionStatus\":\"Active\",\"EditionStatusDescription\":\"Active\",\"OrderItemDeliveryStatusName\":null,\"OrderItemDeliveryStatusDescription\":null,\"OrderItemQuantity\":2,\"OrderItemDeliveryQuantityRequested\":null,\"OrderItemDeliveryID\":null,\"UploadedFlag\":null,\"AccessCode\":null,\"Created\":null,\"UseSecureBrowserFlag\":null,\"ExamName\":\"Cert's Exam\",\"CohortID\":2267,\"CohortName\":\"Cohort test 30 may\",\"StudentCount\":19,\"ExamTypeProgramTypeID\":null,\"OrderItemApplicationID\":2,\"DBLockedByUserName\":null,\"AutoCodeEditionFlag\":null,\"WorkflowName\":null,\"WorkflowStatus\":null,\"UseLastSyllabusFlag\":false,\"SyllabusID\":58,\"AssessmentTypeName\":null}]}]"

有人可以建议一种有效的方法来获得"价值" JArray使用Newtonsoft JSON API吗?

2 个答案:

答案 0 :(得分:1)

这是我尝试过的,它有效。如果有人可以改进它,请随意。

private dynamic GetFirstObject(string response)
{
    dynamic firstObject = null;
    if (!String.IsNullOrEmpty(response))
    {
        JArray arrResponse = JArray.Parse(response) as JArray;
        dynamic dyResponse = arrResponse[0];
        string strValue = JsonConvert.SerializeObject(dyResponse.Value);
        JArray arrValueList = JArray.Parse(strValue);

        if (arrValueList.Count > 0)
        {
            firstObject = arrValueList[0];
        }
    }
    return firstObject;
}

答案 1 :(得分:0)

使用DeserializeObject

var myObject = JsonConvert.DeserializeObject<List<ResponseObject>>(json);
var OrderID = myObject[0].Value[0].OrderID;

以下是您需要的课程:

public class ResponseObject
{
    public string Key { get; set; }
    public List<Value> Value { get; set; }
}

public class Value
{
    public int OrderID { get; set; }
    public int OrderItemID { get; set; }
    public int ProgramID { get; set; }
    public string ProgramName { get; set; }
    public string ProgramDescription { get; set; }
    public string ScheduleDate { get; set; }
    public string SubmitOrderRequestDate { get; set; }
    public string OrderTypeName { get; set; }
    public string OrderTypeDescription { get; set; }
    public object ProductID { get; set; }
    public object ProductName { get; set; }
    public object ProductDescription { get; set; }
    public object ProductHTMLTitle { get; set; }
    public string EditionName { get; set; }
    public int EditionID { get; set; }
    public string EditionDescription { get; set; }
    public object SpecialInstruction { get; set; }
    public string OrderDeliveryTypeName { get; set; }
    public string OrderDeliveryTypeDescription { get; set; }
    public string OrderItemStatusName { get; set; }
    public string OrderItemStatusDescription { get; set; }
    public string EditionStatus { get; set; }
    public string EditionStatusDescription { get; set; }
    public object OrderItemDeliveryStatusName { get; set; }
    public object OrderItemDeliveryStatusDescription { get; set; }
    public int OrderItemQuantity { get; set; }
    public object OrderItemDeliveryQuantityRequested { get; set; }
    public object OrderItemDeliveryID { get; set; }
    public object UploadedFlag { get; set; }
    public object AccessCode { get; set; }
    public object Created { get; set; }
    public object UseSecureBrowserFlag { get; set; }
    public string ExamName { get; set; }
    public int CohortID { get; set; }
    public string CohortName { get; set; }
    public int StudentCount { get; set; }
    public object ExamTypeProgramTypeID { get; set; }
    public int OrderItemApplicationID { get; set; }
    public object DBLockedByUserName { get; set; }
    public object AutoCodeEditionFlag { get; set; }
    public object WorkflowName { get; set; }
    public object WorkflowStatus { get; set; }
    public bool UseLastSyllabusFlag { get; set; }
    public int SyllabusID { get; set; }
    public object AssessmentTypeName { get; set; }
}