How to parse that json response? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am tired about parsing that json response :
I prepared class something for model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Announcements
{
public class Private
{
public string created_at { get; set; }
public int id { get; set; }
public string text { get; set; }
public string title { get; set; }
public string updated_at { get; set; }
public int user_id { get; set; }
}
public class User
{
public string first_name { get; set; }
public int id { get; set; }
public string last_name { get; set; }
}
public class Public
{
public string created_at { get; set; }
public int id { get; set; }
public string text { get; set; }
public string title { get; set; }
public User user { get; set; }
}
public class RootObject
{
public List<Private> #private { get; set; }
public List<Public> #public { get; set; }
}
}
Now time to deserialize response :
var tempUsersArray = JsonConvert.DeserializeObject<Announcements.RootObject>(response.Content);
and this makes a app crash...
Anyone have idea what i do wrong?

You don't define permission for class, please chnge class Announcements to public class Announcements. Now it should work properly, test this tip please.

Related

Nested Json Parsing to c#(Console Application) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I need to parse Json into C# (Console Application) and also need to view the parsed data in a datatable.
I have tried to generate the classes as far as I know.
Code:
{
"RLC": [
{
"PAR": ""
},
{
"PAR": ""
},
{
"PAR": ""
}
],
"PR":
Please help out.
you can use the below c# class to serialize the json
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class RecordLocator
{
public string PNR { get; set; }
}
public class PNRAmount
{
public string BalanceDue { get; set; }
public string AuthorizedBalanceDue { get; set; }
public string SegmentCount { get; set; }
public string PassiveSegmentCount { get; set; }
public string TotalCost { get; set; }
public string PointsBalanceDue { get; set; }
public string TotalPointCost { get; set; }
public List<object> AlternateCurrencyCode { get; set; }
public string AlternateCurrencyBalanceDue { get; set; }
}
public class Root
{
public List<RecordLocator> RecordLocator { get; set; }
public PNRAmount PNRAmount { get; set; }
}

Unable to get third level records filtered using Entity Framework [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have the below code structure and would like to get all the record from Unit collection where AmountIsTaxInclusive is true
public class Unit
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UnitID { get; set; }
public string Name { get; set; }
public int UnitStatusID { get; set; }
public virtual Reservation Reservation { get; set; }
}
public class Reservation
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ReservationID { get; set; }
public int ReservationStatusID { get; set; }
public virtual ICollection<ReservationUnits> ReservationUnits
}
public class ReservationUnit
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ReservationUnitID { get; set; }
[Required]
public int ReservationID { get; set; }
public Reservation Reservation { get; set; }
[Required]
public int UnitID { get; set; }
public Unit Unit { get; set; }
public decimal Amount { get; set; }
public bool AmountIsTaxInclusive { get; set; }
public bool IsFixedRate { get; set; }
}
When I tried to filter Reservationunit collection I am getting all records.
Query
var result = _unitOfWork.UnitRepository.GetBySessionIdAndUnit(sessionId, newUnitIds).IncludeFilter(p => p.Reservation).IncludeFilter(y => y.Reservation.ReservationUnit.Where(c => c.AmountIsTaxInclusive == true)).ToList();
The answer is simple. You just need to make an instance of your Context class(let us name it dbContext).
Then call the following line where you want.
var myDesiredList = dbContext.ReservationUnit.where( ru => ru.AmountIsTaxInclusive == true).ToList();
Let me know if you had more information to share.

How to deserialize strange JSON format in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have this JSON response from a server:
"{\"Data\":[{\"AI\":\"(00)103002310000531111\",\"BatchId\":157,\"LogisticLevel\":7,\"ParentId\":-1,\"State\":1,\"SyncState\":-1,\"InternalID\":86996,\"ModifyReason\":null,\"AggregationDate\":\"1900-01-01T00:00:00\",\"CommissioningDate\":\"1900-01-01T00:00:00\",\"DecommissioningDate\":\"1900-01-01T00:00:00\",\"NumberOfChildren\":0,\"RejectCode\":-1,\"ShippingDate\":\"1900-01-01T00:00:00\",\"TotalNumberOfUnits\":1,\"CompanyPrefix\":\"030023\",\"FilterValue\":7,\"PackLevel\":1,\"ReferenceCode\":\"\",\"Schema\":1,\"SerialNumber\":\"1000053111\",\"IsGood\":true,\"Children\":[]}],\"Code\":10,\"Message\":\"Data retrieved\"}"
My responsbody it's wrong...much slashes...why ?
I serialize data with Newtonsoft JSON.
But the format isn't correct, I receive an error while deserializing on the client.
I saw that there're slashes in the JSON string. How can I solve this?
I use C#.
Your Json is valid and can be deserialized using JsonConvert:
public class Datum
{
public string AI { get; set; }
public int BatchId { get; set; }
public int LogisticLevel { get; set; }
public int ParentId { get; set; }
public int State { get; set; }
public int SyncState { get; set; }
public int InternalID { get; set; }
public object ModifyReason { get; set; }
public DateTime AggregationDate { get; set; }
public DateTime CommissioningDate { get; set; }
public DateTime DecommissioningDate { get; set; }
public int NumberOfChildren { get; set; }
public int RejectCode { get; set; }
public DateTime ShippingDate { get; set; }
public int TotalNumberOfUnits { get; set; }
public string CompanyPrefix { get; set; }
public int FilterValue { get; set; }
public int PackLevel { get; set; }
public string ReferenceCode { get; set; }
public int Schema { get; set; }
public string SerialNumber { get; set; }
public bool IsGood { get; set; }
public List<object> Children { get; set; }
}
public class RootObject
{
public List<Datum> Data { get; set; }
public int Code { get; set; }
public string Message { get; set; }
}
JsonConvert.DeserializeObject<RootObject>(
"{\"Data\":[{\"AI\":\"(00)103002310000531111\",\"BatchId\":157,\"LogisticLevel\":7,\"ParentId\":-1,\"State\":1,\"SyncState\":-1,\"InternalID\":86996,\"ModifyReason\":null,\"AggregationDate\":\"1900-01-01T00:00:00\",\"CommissioningDate\":\"1900-01-01T00:00:00\",\"DecommissioningDate\":\"1900-01-01T00:00:00\",\"NumberOfChildren\":0,\"RejectCode\":-1,\"ShippingDate\":\"1900-01-01T00:00:00\",\"TotalNumberOfUnits\":1,\"CompanyPrefix\":\"030023\",\"FilterValue\":7,\"PackLevel\":1,\"ReferenceCode\":\"\",\"Schema\":1,\"SerialNumber\":\"1000053111\",\"IsGood\":true,\"Children\":[]}],\"Code\":10,\"Message\":\"Data retrieved\"}"
).Dump();
String json= "{\"Data\":[{\"AI\":\"(00)103002310000531111\",\"BatchId\":157,\"LogisticLevel\":7,\"ParentId\":-1,\"State\":1,\"SyncState\":-1,\"InternalID\":86996,\"ModifyReason\":null,\"AggregationDate\":\"1900-01-01T00:00:00\",\"CommissioningDate\":\"1900-01-01T00:00:00\",\"DecommissioningDate\":\"1900-01-01T00:00:00\",\"NumberOfChildren\":0,\"RejectCode\":-1,\"ShippingDate\":\"1900-01-01T00:00:00\",\"TotalNumberOfUnits\":1,\"CompanyPrefix\":\"030023\",\"FilterValue\":7,\"PackLevel\":1,\"ReferenceCode\":\"\",\"Schema\":1,\"SerialNumber\":\"1000053111\",\"IsGood\":true,\"Children\":[]}],\"Code\":10,\"Message\":\"Data retrieved\"}";
dynamic bsObj = JsonConvert.DeserializeObject<dynamic>(json);
Console.WriteLine(bsObj.ToString());
Console.WriteLine(bsObj.Data[0].BatchId.ToString()); //157
Console.WriteLine(bsObj.Code.ToString()); // 10
Console.WriteLine(bsObj.Message.ToString()); // Data retrieved

How do I convert json.net property is contiguous? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
dataModel = [{"ASB_Base_1":0,"ASB_Idx_1":0,"ASB_Name_Idx_1":"non","ASB_Base_2":0,"ASB_Idx_2":0,"ASB_Name_Idx_2":"non","ASB_Base_3":0,"ASB_Idx_3":0,"ASB_Name_Idx_3":"non"},
{"ASB_Base_1":0,"ASB_Idx_1":0,"ASB_Name_Idx_1":"non","ASB_Base_2":0,"ASB_Idx_2":0,"ASB_Name_Idx_2":"non","ASB_Base_3":0,"ASB_Idx_3":0,"ASB_Name_Idx_3":"non"},
{"ASB_Base_1":0,"ASB_Idx_1":0,"ASB_Name_Idx_1":"non","ASB_Base_2":0,"ASB_Idx_2":0,"ASB_Name_Idx_2":"non","ASB_Base_3":0,"ASB_Idx_3":0,"ASB_Name_Idx_3":"non"}]
The above data in the model.The format is different from the name of the key looks.
public class dataModel
{
public int ASB_Base;
public int ASB_Idx;
public int ASB_Name_Idx;
}
void LoadJsonData(string jsonData)
{
List<dataModel> dataModelList = JsonConvert.DeserializeObject<List<dataModel>>(jsonData);
}
I want to bring in the form of a list, what should I do? Please Help!
Your class should be defined like this:
public class dataModel
{
public int ASB_Base_1 { get; set; }
public int ASB_Idx_1 { get; set; }
public string ASB_Name_Idx_1 { get; set; }
public int ASB_Base_2 { get; set; }
public int ASB_Idx_2 { get; set; }
public string ASB_Name_Idx_2 { get; set; }
public int ASB_Base_3 { get; set; }
public int ASB_Idx_3 { get; set; }
public string ASB_Name_Idx_3 { get; set; }
}

The implementation of a multi-state model in MVC

I want to create a QUESTION model in the test system.
The question may be different (with text, with picture, etc)
The answer may be different (textfield, checkboxes, etc)
How to implement this in MVC correctly?
Picture for understanding
Business/Domain Objects:
public class Question {
public int Id { get; set; }
public string Text { get; set; }
public string ImagePath { get; set; }
public IList<Answer> Answers { get; set; }
public Answer CorrectAnswer { get; set; }
}
public class Answer {
public int Id { get; set; }
public string Text { get; set; }
}
View Model:
public class QuestionViewModel {
public Question Question { get; set; }
}

Categories

Resources