如果您对PHPArraytoJSONArrayusingjson_encode和;感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解PHPArraytoJSONArrayusingjson_enc
如果您对PHP Array to JSON Array using json_encode和;感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解PHP Array to JSON Array using json_encode的各种细节,并对;进行深入的分析,此外还有关于AJAX向SpringMVC controller 传JSONArray并将String转换成JSONArray、delphi的TJSONArray出错:undeclare identify 'TJSONArray'...、Flex 数组 Array 用法---gson转换为flex array的方法--两个Array()/as Array、Gson – How to parse JSON Arrays, an array of arrays的实用技巧。
本文目录一览:- PHP Array to JSON Array using json_encode();
- AJAX向SpringMVC controller 传JSONArray并将String转换成JSONArray
- delphi的TJSONArray出错:undeclare identify 'TJSONArray'...
- Flex 数组 Array 用法---gson转换为flex array的方法--两个Array()/as Array
- Gson – How to parse JSON Arrays, an array of arrays
PHP Array to JSON Array using json_encode();
我已经对使用内置json_encode();
函数制作的数组进行了编码。我需要像这样的数组数组格式:
[["Afghanistan",32,12],["Albania",32,12]]
但是,它返回为:
["2":["Afghanistan",32,12],"4":["Albania",32,12]]
如何在不使用任何正则表达式欺骗的情况下删除这些行号?
答案1
小编典典如果您的PHP数组中的数组键不是连续的数字,则json_encode()
必须
使另一个构造成为一个对象,因为JavaScript数组总是连续地进行数字索引。
array_values()
在PHP的外部结构上使用,以丢弃原始的数组键,并将其替换为从零开始的连续编号:
例:
// Non-consecutive 3number keys are OK for PHP// but not for a JavaScript array$array = array( 2 => array("Afghanistan", 32, 13), 4 => array("Albania", 32, 12));// array_values() removes the original keys and replaces// with plain consecutive numbers$out = array_values($array);json_encode($out);// [["Afghanistan", 32, 13], ["Albania", 32, 12]]
AJAX向SpringMVC controller 传JSONArray并将String转换成JSONArray
http://stackoverflow.com/questions/14515785/how-to-convert-json-string-arrray-to-json-array-list
create JSONArray ([{},{},{}]) in JS
var data = []; data.push({ "id" : 1,"val" : $('#projectName').val() }); data.push({ "id" : 2,"val" : $('#description').val() }); data.push({ "id" : 3,"val" : $('#startdate').val() }); data.push({ "id" : 4,"val" : STATUS_NOT_START }); var id = 5; while($("#" + id).length > 0){ data.push({ "id" : id,"val" : $("#" + id).val() }); id++; }
ajax pass JSONArray String to Controller:
$.ajax({ type : "Post",url : "createProject.html",data : "jsonArray=" + JSON.stringify(data) + "&depId=" + depId + "&groId=" + groId + "&objId=" + objId,success : function(response){ var alertText = "Project " + $('#projectName').val() + " is successfully created! Project ID: " + response; adDalert("alert-success",alertText,"#alertdiv"); },error : function(e){ var alertText = 'Error: ' + e; adDalert("alert-error","#alertdiv"); } });
Controller将JSONArray String转换成JSONArray
import net.sf.json.JSONArray; JSONException; JSONObject; JSONSerializer; public class TestJson { static void parseProfilesJson(String the_json){ try { JSONArray nameArray =(JSONArray) JSONSerializer.toJSON(the_json); System.out.println(nameArray.size()); for(Object js : nameArray){ JSONObject json = (JSONObject)js; System.out.println(json.get("date")); } } catch (JSONException e) { e.printstacktrace(); } } void main(String[] args) { String s = "[{\"date\":\"2012-04-23\",\"activity\":\"gym\"},{\"date\":\"2012-04-24\",\"activity\":\"walking\"}]"; parseProfilesJson(s); } }
delphi的TJSONArray出错:undeclare identify 'TJSONArray'...
总结
以上是小编为你收集整理的delphi的TJSONArray出错:undeclare identify ''TJSONArray''...全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
Flex 数组 Array 用法---gson转换为flex array的方法--两个Array()/as Array
如何使用示例
下面的示例演示 Array() 函数在参数不是原始值时的行为。 转换为数组的一种常见用法是转换以数组格式存储值的 Object 实例。 如果调用 Array() 时使用了 Object 类型或其它任何非原始数据类型的参数,则对对象的引用将存储在新数组的元素中。 也就是说,如果传递的唯一参数是一个对象,则对该对象的引用将成为新数组的第一个元素。
var obj:Object = [ "a","b","c" ];
var newArray:Array = Array( obj );
trace(newArray == obj); // false
trace(newArray[0] == obj); // true
trace(newArray[0][0]) // a
trace(newArray[0][1]) // b
trace(newArray[0][2]) // c
若要将 obj 转换为数组,请使用 as 运算符,它将在 obj 为有效数组的情况下返回数组引用,否则返回 null:
var obj:Object = [ "a","c" ];
var newArray:Array = obj as Array;
trace(newArray == obj); // true
trace(newArray[0]); // a
trace(newArray[1]); // b
trace(newArray[2]); // c
Gson – How to parse JSON Arrays, an array of arrays
https://mkyong.com/java/gson-how-to-parse-json-arrays-an-array-of-arrays/


In this article, we will show few JSON array examples and also how to use Gson
to map it back to Java Object.
<!-- Gson JSON -> Object -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
JSON basic.
[ ]
= array{ }
= object
1. JSON array of object.
1.1 Sample.
[
{
"id": 1, "name": "a"
},
{
"id": 2, "name": "b"
}
]
1.2 Gson
convert above JSON array into a List<Item>
.
package com.mkyong;
public class Item {
private int id;
private String name;
}
package com.mkyong;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class JsonArray1 {
public static void main(String[] args) throws IOException {
JavaParseJsonArray main = new JavaParseJsonArray();
Gson gson = new Gson();
Type listType = new TypeToken<List<Item>>() {}.getType();
List<Item> list = gson.fromJson(main.loadFileFromClasspath("array1.json"), listType);
System.out.println(gson.toJson(list));
}
public String loadFileFromClasspath(String fileName) throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream(fileName)) {
// common-io
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
}
}
P.S We use common-io
to convert inputStream
to String
.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
2. JSON array of {object with an array of object}.
2.1 Sample.
[
{
"id": 1,
"name": "a",
"types":
[
{"id": 1,"name": "a1"},
{"id": 2,"name": "a2"}
]
},
{
"id": 2,
"name": "b",
"types":
[
{"id": 1,"name": "b1"},
{"id": 2,"name": "b2"}
]
}
]
2.2 Gson
convert above JSON array into a List<Item>
containing List<ItemType> types
.
package com.mkyong;
import java.util.List;
public class Item {
private int id;
private String name;
private List<ItemType> types;
}
package com.mkyong;
public class ItemType {
private int id;
private String name;
}
2.3 Same code, no need to change.
Type listType = new TypeToken<List<Item>>() {}.getType();
List<Item> list = gson.fromJson(main.loadFileFromClasspath("array2.json"), listType);
3. JSON array of {object with an array of an array of object}.
3.1 Sample.
[
{
"id": 1,
"name": "a",
"types":
[
[
{"id": 1,"name": "a1"},
{"id": 2,"name": "a2"}
],
[
{"id": 3,"name": "a3"}
]
]
},
{
"id": 2,
"name": "b",
"types":
[
[
{"id": 1,"name": "b1"}
],
[
{"id": 2,"name": "b2"}
]
]
}
]
3.2 Change the types
to List<ItemType> types[];
package com.mkyong;
import java.util.List;
public class Item {
private int id;
private String name;
private List<ItemType> types[]; // change types -> types[]
}
package com.mkyong;
public class ItemType {
private int id;
private String name;
}
3.3 Same code, no need to change.
Type listType = new TypeToken<List<Item>>() {}.getType();
List<Item> list = gson.fromJson(main.loadFileFromClasspath("array3.json"), listType);
3.4 See the debugging mode.

4. JSON array of {object with an array of an array of String}.
4.1 This is a bit unusual JSON array sample.
[
{
"id": 1,
"name": "a",
"types":
[
[
"a1", 1
],
[
"a2", 2
]
]
},
{
"id": 2,
"name": "b",
"types":
[
[
"b1", 1
]
]
}
]
4.2 Change the types
to List<String> types[];
package com.mkyong;
import java.util.List;
public class Item {
private int id;
private String name;
private List<String> types[];
}
4.3 Same code, no need to change.
Type listType = new TypeToken<List<Item>>() {}.getType();
List<Item> list = gson.fromJson(main.loadFileFromClasspath("array4.json"), listType);
References
- Wikipedia – JSON
- Gson – How to parse JSON
- Java JSON Tutorials
关于PHP Array to JSON Array using json_encode和;的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于AJAX向SpringMVC controller 传JSONArray并将String转换成JSONArray、delphi的TJSONArray出错:undeclare identify 'TJSONArray'...、Flex 数组 Array 用法---gson转换为flex array的方法--两个Array()/as Array、Gson – How to parse JSON Arrays, an array of arrays的相关信息,请在本站寻找。
本文标签: