protected class saveBtnClickHandler implements OnClickListener{
@Override
public void onClick(View v) {
String jsonRest = loadJsonDataFromURL("http://thirddhaba.appspot.com/api/v1/circle/condensed/data/?circle_id=1");
try {
JSONObject jsonObj = new JSONObject(jsonRest);
} catch (JSONException e) {
android.util.Log.e("JSON Parser", "Error parsing data " + e.toString());
}
}
}
protected String loadJsonDataFromURL(String url){
String jsonStr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
jsonStr = out.toString();
} else{
//Closes the connection. …Run Code Online (Sandbox Code Playgroud) 我刚刚从 URL 请求了一个 JSONObject,我想知道两件事。JSONObject 是否类似于 JSONArray?我应该要求哪个?有没有办法像var_dump在 PHP 中一样显示这个 JSONObject ?只是为了检查它是否有效?
非常感谢帮助!菲利普
我试图从事先收到的JSON代码中搜索..
我想知道有没有有效的方法来搜索特定的对象?或者我应该像循环和if语句一样使用直接搜索?
我已成功解析JSON,并将其存储在我的String变量中.. JSON包含对象数组..
现在我想搜索特定的ObjectName并获取整个对象.. JSON就像
[
{
"name":"blank",
"date":"2014-06-05T00:44:30Z",
"boolean":null,
},
{
"name":"hello",
"date":"2013-05-04T00:43:20Z",
"boolean":null,
}
]
Run Code Online (Sandbox Code Playgroud)
我想搜索name ="hello"并返回最后一个Array
谁能告诉我怎么样?
非常感谢!
我想在我的网站上创建gmaps.
我找到了,如何获得坐标.
{
"results" : [
{
// body
"formatted_address" : "Pu?awska, Piaseczno, Polska",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.0979041,
"lng" : 21.0293984
},
"southwest" : {
"lat" : 52.0749265,
"lng" : 21.0145743
}
},
"location" : {
"lat" : 52.0860667,
"lng" : 21.0205308
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 52.0979041,
"lng" : 21.0293984
},
"southwest" : {
"lat" : 52.0749265,
"lng" : 21.0145743
}
}
},
"partial_match" …Run Code Online (Sandbox Code Playgroud) 问题是,我的JSON字符串如下所示:
jsonString = [["1","100"],["2","200"],["3","300"]]
Run Code Online (Sandbox Code Playgroud)
我需要用Java制作一个二维数组.如果我写
JSONObject jObs = new JSONObject(jsonString);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
A JSONObject text must begin with '{' at character 1 of [["1 ...
Run Code Online (Sandbox Code Playgroud)
如何解析这个字符串中的二维数组?提前致谢.
如何在 Java 中使用 JSONObject 将键的值设置为整数?我可以使用设置字符串值JSONObject.put(a,b);
但是,我无法弄清楚如何使用.put()设置整数值。例如:我希望我的 jsonobject 看起来像这样:
{"age": 35}
而不是
{"age": "35"}.
我有JSON文件,我需要再次阅读,编辑和写出.
阅读工作正常,我在数据中与JSON数组的写入部分斗争.
我使用JSON.simple库来处理Java中的JSON.
该文件如下所示:
{
"maxUsers":100,
"maxTextLength":2000,
"maxFileSize":2000,
"services":
[
{
"serviceName":"?????c",
"className":"YandexConnector.class",
"isEnabled":true
},
{
"serviceName":"Google",
"className":"GoogleConnector.class",
"isEnabled":false
}
]
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将JSON数据(变量obj)写入文件时,该services数组被破坏.我的写作代码:
JSONObject obj = new JSONObject();
obj.put("maxUsers", this.getMaxUsers());
obj.put("maxTextLength", this.getMaxTextLength());
obj.put("maxFileSize", this.getMaxFileSize());
JSONArray servicesJSON = new JSONArray();
ArrayList<Service> servicesArray = this.getServices();
for(int i=0; i< servicesArray.size(); i++)
{
servicesJSON.add(servicesArray.get(i));
}
obj.put("services", servicesJSON);
FileWriter file = new FileWriter(filename);
obj.writeJSONString(file);
file.flush();
file.close();
Run Code Online (Sandbox Code Playgroud)
这输出:
{
"services":
[
translator.settings.Service@121c5df,
translator.settings.Service@45f4ae
],
"maxTextLength":2000,
"maxUsers":100,
"maxFileSize":2000
}
Run Code Online (Sandbox Code Playgroud)
如何将JSON数据正确地写入文件,如果我在JSONArray中这样做services …
我们目前使用的是 Java EE 5,我们在发送响应之前执行以下操作将 POJO 转换为 JSON。
@GET
@Path("/books")
@Produces(MediaType.APPLICATION_JSON)
public Response getBooks() {
List<Book> listOfBooks = getMiscService().getbooks();
String response = "{\"books\":" + gson.toJson(listOfBooks) + "}";
return Response.status(Response.Status.OK).entity(response).build();
}
Run Code Online (Sandbox Code Playgroud)
我们正在使用谷歌的 gson API。现在我们正在将代码重组为符合 Java EE 7 API 的,我想知道是否有任何 JSON 转换 API 可以将 POJO 转换为 JSON。
我知道 Java EE 7 中引入的 JsonObject API。但我仍然想知道如何获得我的 POJO 的 JSON 表示。
JsonObject jsonObject = Json.createObjectBuilder().add("books", myObject);
Run Code Online (Sandbox Code Playgroud)
上面的 myObject 需要是我的对象的 JSON 表示正确吗?
我正在考虑这个。但这仍然使用 Gson
JsonObject jsonObject = Json.createObjectBuilder().add("books", gson.toJson(myObject));
Run Code Online (Sandbox Code Playgroud)
这里推荐的方式是什么?
谢谢
我有一个JSONArray如下.如何按顺序访问其中的每个键和值.
JSONArray = [{"a":1},{"b":2,"c":3},{"d":4},{"e":5,"f":7}]
Run Code Online (Sandbox Code Playgroud) 在谷歌搜索我发现这个解决方案从URL读取json字符串:
JSONObject json = new JSONObject(IOUtils.toString(new URL("https://somelink.com"), Charset.forName("UTF-8")));
Run Code Online (Sandbox Code Playgroud)
问题是没有JSONObject(String)构造函数,为什么会这样?
jsonobject ×10
json ×9
java ×6
android ×3
android-json ×1
arrays ×1
gson ×1
integer ×1
iterator ×1
jakarta-ee ×1
java-ee-7 ×1
json-simple ×1
parsing ×1
put ×1
search ×1
string ×1
url ×1