如何从一次JSONString.stringify简单数组解析JSONString,该数组现在“显得扁平化”,并将其值转换回Java List或Java Array? (使用Jersey 1.x& Java)?数组最初在字符串化之前以[1,2,3]开始。
items =(3)[“To”,“8357”,“30028”] - >通过休息呼叫发送的JSON.stringify(项目)
Chrome Dev Tools在休息电话后的请求有效负载: 项=%5B%22%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0 %C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2 %A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0To%22%2C%228357%22%2C%2230028%22%5D
/*inside (Jersey) Rest Resource
@POST
@Path("/...")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response receive(@Context SecurityContext securityContext, @Context
HttpServletRequest srequest, String jsonString) throws URISyntaxException,
JSONException ...
/ * eclipse监视jsonString里面(Jersey)Rest Resource 项=%5B%22%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0 %C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2 %A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0To%22%2C%228357%22%2C%2230028%22%5D * / [在此输入图像说明] [2]
注意:没有名称值。没有实体。
只有一个非常简单的ID字符串,因为这就是我所需要的。 (Jersey 1.X或JAX-RS 1.X支持吗?)
JSONArray jSONArray = new JSONArray(java.util.Arrays.asList(jsonString));
Eclipse jSONArray表达式:jSONArray
--myArrayList
---- elementData中
------ [0]“项目=%5B%22%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2 %A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0 %C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0To%22%2C%228357%22%2C%2230028%22 %5D“----------值
------------ [0..99]
--------------- [0]我
--------------- 1 t
--------------- [2] e
--------------- [3] m
--------------- [4] s
--------------- [5] =
--------------- [6]%
--------------- [7] 5
--------------- [8] B ....
答案 0 :(得分:0)
我无法完全理解你原来的问题,但其中一个方法是:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JsonParser {
JFileChooser chooser = new JFileChooser();
File f;
static String fn = "";
static String js1 = "{\"name\": \"LALA\", \"email\": \"tst@tst.com\"}";
String name = "name";
String email = "email";
String fName = "firsName";
String city = "city";
// ... other needed fields
User u1 = null;
public JsonParser() {
parseFile();
System.out.println("\n" + u1.toShortString());
}
private String openFchooser() throws FileNotFoundException, IOException, InterruptedException, Exception {
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
f = chooser.getSelectedFile();
}
return f.getAbsolutePath();
}
// To parse JSON files with data
//===========================================
public void parseFile() {
JSONParser parser = new JSONParser();
try {
// To parse obj 1
Object obj1 = parser.parse(js1);
System.out.println("User 1: " + obj1.toString());
System.out.println();
JSONObject jobj1 = (JSONObject) obj1;
String from_name = jobj1.get(name).toString();
String from_email = jobj1.get(email).toString();
// String from_fName = jobj1.get(fName).toString();
// String from_city = jobj1.get(city).toString();
u1 = new User(from_name, from_email, null, null);
// System.out.println(u1.toString() + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new JsonParser();
}
class User {
String name = null;
String email = null;
String fName = null;
String city = null;
public User(String n, String e, String f, String c) {
this.name = n;
this.email = e;
this.fName = f;
this.city = c;
}
public String getFirsName() {
return this.name;
}
public String setFirsName(String s) {
return this.name = s;
}
public String getEmail() {
return this.email;
}
public String setEmail(String s) {
return this.email = s;
}
public String toString() {
return "{\"name\":" + this.name + ", "
+ "\"email\":" + this.email + ", "
+ "\"firsName\":" + this.fName + ", "
+ "\"city\":" + this.city + "\"}";
}
public String toShortString() {
return "{\"name\": \"" + this.name + "\", "
+ "\"email\": \"" + this.email + "\"}";
}
};
}
<强>输出:强>
User 1: {"name":"LALA","email":"tst@tst.com"}
{"name": "LALA", "email": "tst@tst.com"}
答案 1 :(得分:0)
谢谢你们。我设法找到另一种发送和放大器接收:
现在首先发送数组而不进行字符串化 收到: public Response archiveSelectedApplicants(@Context SecurityContext securityContext,@ Context HttpServletRequest srequest, @FormParam(&#34; items []&#34;)List items)抛出URISyntaxException