Home > Back-end >  Java how to access the json data in every remark, strives for the code
Java how to access the json data in every remark, strives for the code

Time:09-18

{
"RouteResponse" : {
"Mailno" : "SF1011789832950,"
"Route" : [
{
"Remark" : "the suitable abundant speed luck has already charge express",
"Accept_time" : "the 2020-06-28 09:35:26,"
"Accept_address", "shenzhen",
"Opcode" : "50"
},
{
"Remark" : "express in [new tang industrial park of shenzhen baoan sales] has been loading, ready to send to the next stop",
"Accept_time" : "the 2020-06-28 09:35:46,"
"Accept_address", "shenzhen",
"Opcode" : "30"
},
{
"Remark" : "express to [Shanghai big six project operating point]",
"Accept_time" : "the 2020-06-28 09:35:52,"
"Accept_address", "Shanghai",
"Opcode" : "31"
},
{
"Remark" : "express in six projects business point] [Shanghai already loading, ready to send to the next stop",
"Accept_time" : "the 2020-06-28 09:35:59,"
"Accept_address", "Shanghai",
"Opcode" : "30"
},
{
"Remark" : "express to [Shanghai minhang Cao Xing business point]",
"Accept_time" : "the 2020-06-28 09:36:05,"
"Accept_address", "Shanghai",
"Opcode" : "31"
}
]
}
}

CodePudding user response:

Fixed this format, you can use first judgment "remark" field appears several times, and then as a loop body cycles I,
Then in every loop, do a String of interception, get '" remark ":" ", "" accept_time" before the interval range class content, coexist in a String, and then add to List List, at the end of the loop body, '" remark "' and '" accept_time "with the replace (STR,"'), replace ", in this way, the next time when to get the loop body, she will go for a second

CodePudding user response:

Parsed into a json object and then remove the Route to convert jaonArray, traverse the array, then remark the key values for each element

CodePudding user response:

Public static void main (String [] args) {
//json for com. Alibaba. Fastjson
//your json string
String data="https://bbs.csdn.net/topics/{/"RouteResponse":{"mailno":"SF1011789832950","Route":[{"remark":"顺丰速运 已收取快件","accept_time":"2020-06-28 09:35:26","accept_address":"深圳市","opcode":"50"},{"remark":"快件在[深圳宝安新唐工业园营业部]已装车,准备发往下一站","accept_time":"2020-06-28 09:35:46","accept_address":"深圳市","opcode":"30"},{"remark":"快件到达 [上海大六项目营业点]","accept_time":"2020-06-28 09:35:52","accept_address":"上海市","opcode":"31"},{"remark":"快件在[上海大六项目营业点]已装车,准备发往下一站","accept_time":"2020-06-28 09:35:59","accept_address":"上海市","opcode":"30"},{"remark":"快件到达 [上海闵行曹行营业点]","accept_time":"2020-06-28 09:36:05","accept_address":"上海市","opcode":"31"}]}}";
//json formatted
JSONObject JSONObject=JSON. ParseObject (data);
//get RouteResponse value
JSONObject routeResponse=JSONObject. GetJSONObject (" routeResponse ");
//get the Route array
JSONArray routes=routeResponse. GetJSONArray (" Route ");
//traversal
For (Object route: routes) {
//the element json formatted
JSONObject jsonObject1=JSON. ParseObject (route. The toString ());
//for each remark
String remark=jsonObject1. Get String (" remark ");
System. The out. Println (remark);
}
}

CodePudding user response:

CodePudding user response:

 
String STR="{\ n" +
RouteResponse \ ": {\ '\' n +
"SF1011789832950 mailno "" \ ": " \ ", \ n +
""" Route: [\ n \ "" +
"{\ n +"
Remark "" \ ", \ "suitable abundant speed luck charge express ", \ n +
"Accept_time "" \ ", \ "the 2020-06-28 09:35:26 ", \ n +
""" accept_address \ ": " shenzhen \ ", \ n +
"Opcode "" \ ", \ "" \ n "50 +
"}, \ n + "
"{\ n +"
"" remark \ ": " express in [new tang industrial park of shenzhen baoan sales] has loading, ready to send to the next stop \ ", \ n +
"Accept_time "" \ ", \ "the 2020-06-28 09:35:46 ", \ n +
""" accept_address \ ": " shenzhen \ ", \ n +
"Opcode \ '\' \ ", \ "30" \ n + "
"}, \ n + "
"{\ n +"
"" remark \ ": " express to [Shanghai big six project operating point] \ ", \ n +
"Accept_time "" \ ", \ "the 2020-06-28 09:35:52 ", \ n +
""" accept_address \ ": " \ ", \ n ", Shanghai +
Opcode "" \ ", \ "" \ n "+ 31
"}, \ n + "
"{\ n +"
"" remark \ ": " express in six projects business point] [Shanghai already loading, ready to send to the next stop \ ", \ n +
"Accept_time "" \ ", \ "the 2020-06-28 09:35:59 ", \ n +
""" accept_address \ ": " \ ", \ n ", Shanghai +
Opcode \ '\' \ ", \ "30" \ n + "
"}, \ n + "
"{\ n +"
Remark "" \ ", \ "express to [Shanghai minhang Cao Xing business point] ", \ n +
"Accept_time "" \ ", \ "the 2020-06-28 09:36:05 ", \ n +
""" accept_address \ ": " \ ", \ n ", Shanghai +
Opcode "" \ ", \ "" \ n "+ 31
"} \ n + "
"] \ n + "
"} \ n + "
"} ";
Try {
List List=new ArrayList<> (a);
JSONObject Jo=JSON. ParseObject (STR);
JSONObject routeResponse=JSON. ParseObject (Jo. Get string (" routeResponse "));
JSONArray route=JSON. ParseArray (routeResponse. Get string (" route "));
For (int I=0; I & lt; The route. The size (); I++) {
JSONObject j=(JSONObject) route. Get (I);
List. The add (j.g etString (" remark "));
}
List. The forEach (System. Out: : println);

} the catch (Exception e) {
e.printStackTrace();
}


Results:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related