java中的JSON.stringify - android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22077955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:27:28  来源:igfitidea点击:

JSON.stringify in java - android

javaandroidjsonstringify

提问by ???? ????

Is there any way to perform a JSON.stringifyin android?

有没有办法JSON.stringify在android中执行a ?

I keep seeing JSON.stringify(JSONObject)all around the web, but I cant find the JSON class in android.

我一直JSON.stringify(JSONObject)在网上看到所有内容,但在 android 中找不到 JSON 类。

Any help?

有什么帮助吗?

采纳答案by MH.

JSON.stringify(JSONObject)is a Javascript function and will not be available in Java. If you're using the org.json.*package built in the Android SDK, the equivalent would be to simply call toString()on your JSONObjectinstance, or the more human-friendly toString(int).

JSON.stringify(JSONObject)是一个 Javascript 函数,在 Java 中不可用。如果您使用的org.json.*是 Android SDK 中内置的包,则相当于简单地调用toString()您的JSONObject实例,或者更人性化的toString(int).

http://developer.android.com/reference/org/json/JSONObject.html#toString()http://developer.android.com/reference/org/json/JSONObject.html#toString(int)

http://developer.android.com/reference/org/json/JSONObject.html#toString() http://developer.android.com/reference/org/json/JSONObject.html#toString(int)

JSONObject obj = ...
String jsonString = obj.toString(4);

回答by nikis

Basic actions with JSON objects in JAVA can be done either with a help of org.jsonpackage (included in Android SDK) or javax.json(part of JAVA EE). Both of them have toString()method for conversion of JSONObject to string:

JAVA 中 JSON 对象的基本操作可以在org.json包(包含在 Android SDK 中)或javax.json(JAVA EE 的一部分)的帮助下完成。它们都有toString()将 JSONObject 转换为字符串的方法:

//assuming you have object `jsonobject` of class `JSONObject`
String output = jsonobject.toString()

回答by f10orf12

I know this is old, but I ran into the same problem. And there doesn't seem to be much about it here... so I thought I would add what I learned.

我知道这很旧,但我遇到了同样的问题。这里似乎没有太多关于它的东西......所以我想我会添加我学到的东西。

I used a third-party library to aid in the endeavor: org.codehaus.HymansonAll of the downloads for this can be found here.

我使用了第三方库来帮助完成这项工作:org.codehaus.Hymanson可以在此处找到所有相关下载。

For base JSON functionality, you need to add the following jars to your project's libraries: Hymanson-mapper-asland Hymanson-core-asl

对于基本 JSON 功能,您需要将以下 jar 添加到您的项目库中: Hymanson-mapper-aslHymanson-core-asl

Choose the version your project needs. (Typically you can go with the latest stable build).

选择您的项目需要的版本。(通常您可以使用最新的稳定版本)。

Once they are imported in to your project's libraries, add the following importlines to your code:

将它们导入到您的项目库后,将以下行添加import到您的代码中:

 import org.codehaus.Hymanson.JsonGenerationException;
 import org.codehaus.Hymanson.map.JsonMappingException;
 import org.codehaus.Hymanson.map.ObjectMapper;`

With the java object defined and assigned values that you wish to convert to JSON and return as part of a RESTful web service

使用 Java 对象定义并分配您希望转换为 JSON 并作为 RESTful Web 服务的一部分返回的值

User u = new User();
 u.firstName = "Sample";
 u.lastName = "User";
 u.email = "[email protected]";

ObjectMapper mapper = new ObjectMapper();

try {
    // convert user object to json string and return it 
    return mapper.writeValueAsString(u);
}

  // catch various errors
  catch (JsonGenerationException e) {
    e.printStackTrace();
} 
  catch (JsonMappingException e) {
    e.printStackTrace();
}

The result should looks like this: {"firstName":"Sample","lastName":"User","email":"[email protected]"}

结果应如下所示: {"firstName":"Sample","lastName":"User","email":"[email protected]"}