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

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; }
}

Related

How can I get to c# all randomized profile name in json file using NewtonSoft? [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 4 months ago.
Improve this question
This is my json data
{
"profiles": {
"578ed5c5bab24186a6b0f7cd9a952e10": {
"name": "as",
"type": "custom",
"created": "2022-11-02T09:56:53+03:00",
"lastUsed": "2022-11-02T09:56:53+03:00"
},
"41f62754d535457db1c353872a8aafcf": {
"name": "(Default)",
"type": "custom",
"created": "2022-11-02T09:56:50+03:00",
"lastUsed": "2022-11-02T09:56:50+03:00"
}
},
"selectedProfile": "578ed5c5bab24186a6b0f7cd9a952e10",
"settings": {
"enableAdvanced": false,
"profileSorting": "byName"
},
"version": 3
}
I want to get random numberized 578ed5c5bab24186a6b0f7cd9a952e10 profile names.
Everytime will be updated this data. That's why, I want to filter to profile names.
I want to get profile names values in profiles.
In normally I can access datas with this way
public class _41f62754d535457db1c353872a8aafcf
{
public string name { get; set; }
public string type { get; set; }
public DateTime created { get; set; }
public DateTime lastUsed { get; set; }
}
public class _578ed5c5bab24186a6b0f7cd9a952e10
{
public string name { get; set; }
public string gameDir { get; set; }
public string type { get; set; }
public DateTime created { get; set; }
public DateTime lastUsed { get; set; }
}
public class Profiles
{
public _578ed5c5bab24186a6b0f7cd9a952e10 _578ed5c5bab24186a6b0f7cd9a952e10 { get; set; }
public _41f62754d535457db1c353872a8aafcf _41f62754d535457db1c353872a8aafcf { get; set; }
}
public class Root
{
public Profiles profiles { get; set; }
public string selectedProfile { get; set; }
public Settings settings { get; set; }
public int version { get; set; }
}
public class Settings
{
public bool enableAdvanced { get; set; }
public string profileSorting { get; set; }
}
I want to get without calling class Profiles,class _578ed5c5bab24186a6b0f7cd9a952e10 and public _41f62754d535457db1c353872a8aafcf
I want to get with like public {0} profiles { get; set; }//after profile firt code class
you can use a Dictionary
public class Root
{
public Dictionary<string,Profile> profiles { get; set; }
public string selectedProfile { get; set; }
public Settings settings { get; set; }
public int version { get; set; }
}
public class Profile
{
public string name { get; set; }
public string gameDir { get; set; }
public string type { get; set; }
public DateTime created { get; set; }
public DateTime lastUsed { get; set; }
}
or you can create a list of Profile as a profiles
var jsonObject = JObject.Parse(json);
Data data = jsonObject.ToObject<Data>();
data.profiles = ((JObject)jsonObject["profiles"]).Properties().Select(d => ConvertToProfile(d)).ToList();
public Profile ConvertToProfile(JProperty jProp)
{
var profile = jProp.Value.ToObject<Profile>();
profile.Id = jProp.Name;
return profile;
}
public class Data
{
[JsonIgnore]
public List<Profile> profiles { get; set; }
public string selectedProfile { get; set; }
public Settings settings { get; set; }
public int version { get; set; }
}
public class Profile
{
public string Id { get; set; }
public string name { get; set; }
public string gameDir { get; set; }
public string type { get; set; }
public DateTime created { get; set; }
public DateTime lastUsed { get; set; }
}

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; }
}

Deserialize JSON Data by JSON.Net [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 8 years ago.
Improve this question
I'm now trying to process some JSON data by using JSON.NET
but i have no idea how to solve it.
The JSON String is very long, so i make it a file click me
My goal is to deserialize to an array what type is bikeData
public class bikeData
{
public string sno { get; set; }
public string sna { get; set; }
public string sarea { get; set; }
public string ar { get; set; }
public string tot { get; set; }
public string sbi { get; set; }
public string bemp { get; set; }
public float lat { get; set; }
public float lng { get; set; }
public string mday { get; set; }
public string sv { get; set; }
public bikeData()
{
}
}
Hope anyone can help me.
Try:
JsonConvert.DeserializeObject<IDictionary<string, bikeData>>()
This should do the job for you:
vara bikeDataObjects = JsonConvert.DeserializeObject<List<bikeData>>(jsonString);

How to parse that json response? [closed]

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.

Categories

Resources