How to convert jsonString to JSONObject in Java
Using org.json library:
try {
JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
Log.d("Error", err.toString());
}
Convert JS object to JSON string
All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:
var j = { "name": "binchen"};console.log(JSON.stringify(j));
Converting a string to JSON object
var obj = JSON.parse(string);
Where string
is your json string.
How to convert the following json string to java object?
No need to go with GSON for this; Jackson can do either plain Maps/Lists:
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);
or more convenient JSON Tree:
JsonNode rootNode = mapper.readTree(json);
By the way, there is no reason why you could not actually create Java classes and do it (IMO) more conveniently:
public class Library {
@JsonProperty("libraryname")
public String name;
@JsonProperty("mymusic")
public List<Song> songs;
}
public class Song {
@JsonProperty("Artist Name") public String artistName;
@JsonProperty("Song Name") public String songName;
}
Library lib = mapper.readValue(jsonString, Library.class);
How do I turn a C# object into a JSON string in .NET?
Please Note
Microsoft recommends that you DO NOT USE JavaScriptSerializer
See the header of the documentation page:
For .NET Framework 4.7.2 and later versions, use the APIs in the System.Text.Json namespace for serialization and deserialization. For earlier versions of .NET Framework, use Newtonsoft.Json.
Original answer:
You could use the JavaScriptSerializer
class (add reference to System.Web.Extensions
):
using System.Web.Script.Serialization;
var json = new JavaScriptSerializer().Serialize(obj);
A full example:
using System;
using System.Web.Script.Serialization;
public class MyDate
{
public int year;
public int month;
public int day;
}
public class Lad
{
public string firstName;
public string lastName;
public MyDate dateOfBirth;
}
class Program
{
static void Main()
{
var obj = new Lad
{
firstName = "Markoff",
lastName = "Chaney",
dateOfBirth = new MyDate
{
year = 1901,
month = 4,
day = 30
}
};
var json = new JavaScriptSerializer().Serialize(obj);
Console.WriteLine(json);
}
}
Safely turning a JSON string into an object
JSON.parse(jsonString)
is a pure JavaScript approach so long as you can guarantee a reasonably modern browser.
Related Topics
Replace a Regex Capture Group with Uppercase in JavaScript
Regex.Test V.S. String.Match to Know If a String Matches a Regular Expression
Expose Jquery to Real Window Object with Webpack
Get Caret (Cursor) Position in Contenteditable Area Containing HTML Content
Using the Get Parameter of a Url in JavaScript
Regex to Check Whether a String Contains Only Numbers
Encrypt in PHP Openssl and Decrypt in JavaScript Cryptojs
HTML Alignment Issue in One MAChine Only (Both IE8)
Embed an External Page Without an Iframe
Session Only Cookies with JavaScript
How to Delete Document from Firestore Using Where Clause
How to Access Iframe Parent Page Using Jquery
Check If Url Scheme Is Supported in JavaScript
Getting Blob Data from Xhr Request
JavaScript Closures and 'This'
How Does Shallow Compare Work in React
Get the Content (Text) of an Url After JavaScript Has Run with PHP
How to Run HTML Files Directly from Github, Instead of Just Viewing Their Source