我们如何在Java中对JSON对象进行编码?
JSONObject是java.util.HashMap的子类,其中不提供顺序。我们还可以在 JSONValue.toJSONString(map) 方法的帮助下使用元素的严格排序,即通过 java.util.LinkedHashMap 的实现。 p>
我们可以在下面的两个示例中对 JSON 对象进行编码。
示例
import java.util.*; import org.json.simple.JSONObject; public class JSONEncodingTest { public static void main(String[] args) { Map dataMap = new HashMap(); dataMap.put("Name", "Adithya"); dataMap.put("Age", new Integer(25)); dataMap.put("Salary", new Double(25000.00)); dataMap.put("Employee Id", new Integer(115)); dataMap.put("Company", "TutorialsPoint"); JSONObject jsonObj = new JSONObject(dataMap); System.out.print("Encoding a JSON object: "); System.out.print(jsonObj); } }登录后复制