How to update property inside a object in C# [closed] - c#

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
How can I update "UserPhoneNumber" property value in "Data" property alone from this object. I have done only to change value of property and not nested object.
var updateProfile = generalSettingResponse.Select(x => settingsList.ContainsKey(x.Key) ? settingsList.First(y => y.Key.ToLower() == x.Key) : x);
sample json object:
{
"Name": "John",
"SNo": "1234",
"Data": {
"UserPhoneNumber": "9102287287",
"UserProfileName": "John"
}
}

If you want to change property of json object,here is a demo:
SampleJObject:
{ "Name": "John", "SNo": "1234", "Data": { "UserPhoneNumber": "9102287287", "UserProfileName": "John" } }
Use the following code,the property UserPhoneNumber will be changed:
SampleJObject["Data"]["UserPhoneNumber"] = "0123456789";
result:

You cannot use this:
updateProfile.Data.UserPhoneNumber = "+18888888888";
That will only work if the setters in the class are public.

Related

Read information from a dictionary [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
How can I read information from a dictionary to a text box?
var cities = new Dictionary<int, string>
{
{ 10, "New York" },
};
textBox1.Text = cities.Values[1];
Try instead to use the index that you created on the dictionary:
var cities = new Dictionary<int, string>
{
{ 10, "New York" },
};
textBox1.Text = cities[10];

Which way is better to map a string array and null check? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
What is the best way to map the result of adds to Codes?
result.adds contains a string array:
string[] adds = new string[] { "A", "B", "C" };
Option 1:
var car = new Car()
{
Name = request.Name,
Codes = (result.adds != null) ? adds : new string[] { }
};
In my opinion option 1 seems strange with creating a new empty string array when the result is false. I also have my doubts about mapping within the object mapping itself. On the other hand, it's direct and fast.
Option 2:
if (result.adds != null)
{
adds = results.adds;
}
var car = new Car()
{
Name = request.Name,
Codes = adds
};
Option 2 null check seems overkill to define the code seperately. It could simply be included in the mapper.
Of the two options you've presented option #1 looks the cleanest. However, you can simplify it more if you can use the null coalescing operator:
var car = new Car()
{
Name = request.Name,
Codes = result.adds ?? Array.Empty<string>()
};
This will use an empty string array in results.add is null.

Ways to parse Json recursive [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 3 years ago.
Improve this question
My problem is that I need to parse json to another json format, I need to do that recursive. I have been tried diferents way but any works fine for me.
I would like to know more ways for test.
Thanks!
{
"items": {
"itemName": {
"type": "type",
"properties": {
"item1": {
"type": 1,
"isValid": true
},
"item2": {
"type": 1,
"isValid": true
}
}
}
}
}
And I need to make this
{
"items":{
"item1": 1,
"item2": 1
}
}
You can try with JToken to read the JSON and JsonConvert to convert the object to desired JSON
using (StreamReader r = new StreamReader(filepath))
{
var inputString = r.ReadToEnd();
JToken outer = JToken.Parse(inputString);
JObject inner = outer["items"]["itemName"]["properties"].Value<JObject>();
List<string> keys = inner.Properties().Select(p => p.Name).ToList();
var items = new ExpandoObject() as IDictionary<string, Object>;
foreach (string k in keys)
{
items.Add(k, Convert.ToInt32(outer["items"]["itemName"]["properties"][k]["type"]));
}
Console.WriteLine(JsonConvert.SerializeObject(new { items = items }));
}
output
{"items":{"item1":1,"item2":2}}

Parse JSON using Newtonsoft.Json C# [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 4 years ago.
Improve this question
I have the following json string:
{
"data":
{
"id": "1",
"city": "London"
},
"cityDetails":
{
"_id": "1",
"location": "UK",
"th": 0,
"title": "Default Group",
}
},
"limit": 0.60451203584671021,
"_id": "1234"
}
How can I extract the the 'city' name from the 'data' section of the JSON string using Newtonsoft.Json in C#.
Try
// load your json here
var obj = JObject.Parse(#"{
""data"":
{
""id"": ""1"",
""city"": ""London""
},
""_id"": ""1234""
}");
// get the city
var city = (string)obj.SelectToken("data.city");
You need to update the selected-token path if the JSON you provided is part of/inside another.

How to run any mongodb query from C# [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 8 years ago.
Improve this question
I want to run this mongodb query from C#:
db.Stories.find({_id: ObjectId("52318492c28f7c19d4270c7f")},
{Chapter:
{$elemMatch:{
ChapId:ObjectId("52318629c28f7c1ec4515000")
}}
})
Try to make something like this:
var server = MongoServer.Create("mongodb://localhost:27020");
var database = server.GetDatabase("someDb");
var collection = database.GetCollection<Type>("someCollection");
var searchQuery = Query.GT("myfield", 10).LT(20);
var list = collection.Find(searchQuery);
Reference: MongoDB C# Query expression (How to?)
What does Query.GT mean
My collection like this
And i want to get The chapcontent in a chapter by chapid
"AuthorName" : "From",
"CateName" : "biết chết liền",
"Chapter" : [
{
"ChapContent" : [
{
"ImgUrL" : "../Upload/Stories/523675fec28f7c04b088afd6/52367611c28f7c04b088afd7/12613_617411151612439_796025785_n.jpg",
"Page" : 1,
"PageId" : ObjectId("52367612c28f7c04b088afd8")
},
{
"ImgUrL" : "../Upload/Stories/523675fec28f7c04b088afd6/52367611c28f7c04b088afd7/21453_617411254945762_2771189_n.jpg",
"Page" : 1,
"PageId" : ObjectId("52367612c28f7c04b088afd9")
},
"DatePost" : ISODate("2013-09-16T03:07:42.096Z"),
"Description" : "dsngdsfhgdsjhfgds",
"FirstCharacter" : "b",
"LastModify" : ISODate("2013-09-16T03:07:42.096Z"),
"Status" : 1,
"StoryName" : "biết ai",
"Title" : "ai biêt",
"_id" : ObjectId("523675fec28f7c04b088afd6")

Categories