我现在一个程序是siebel的输入格式转成json,但是遍历时紧急联系人个电话这个集合只输出一个其他的不出来

{"phone":"18610217536","name":"实际司机1","vehicalModel":"","extenalId":"LRDV6PDC2JL605998","vehicalSeries":"拓陆者S系列","vehicleBrand":"拓陆者","vehicalType":"03","memberEmergencyContact":{"phone":"18610217536","name":"紧急联系人1"}}`

package com.siebel.json;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.siebel.data.SiebelPropertySet;
import com.siebel.eai.SiebelBusinessService;
import com.siebel.eai.SiebelBusinessServiceException;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("unused")
public class EAIJSONConverter
  extends SiebelBusinessService
{
    @Override
    public void doInvokeMethod(String methodName, SiebelPropertySet input, SiebelPropertySet output)
    throws SiebelBusinessServiceException
  {
    if (methodName.equals("PropSetToJSON"))
    {
      JsonObject myJSON = new JsonObject();
      myJSON = PropertySetToJsonObject(input, myJSON);
      output.setValue(myJSON.toString());
    }
    if (methodName.equals("JSONToPropSet"))
    {
      JsonObject obj = new JsonObject();
      obj = (JsonObject)new Gson().fromJson(input.getValue(), JsonObject.class);
      JsonObjectToPropertySet(obj, output);
    }
  }

  @SuppressWarnings("rawtypes")
public static SiebelPropertySet JsonObjectToPropertySet(JsonObject obj, SiebelPropertySet ps)
  {

    Iterator<Map.Entry<String, JsonElement>> iterator = obj.entrySet().iterator();
    while (iterator.hasNext())
    {
      JsonArray jsonArray = new JsonArray();
      JsonObject jsonObject = new JsonObject();     
      Map.Entry mapEntry = (Map.Entry)iterator.next();
     // Map.Entry mapEntry = iterator.next();
      if (mapEntry != null)
      {
        JsonElement jsonelement = (JsonElement)mapEntry.getValue();
        if (jsonelement.isJsonArray())
        {
          jsonArray = jsonelement.getAsJsonArray();
          SiebelPropertySet child = new SiebelPropertySet();
          child.setType("ListOf-" + mapEntry.getKey().toString());
          SiebelPropertySet temp = new SiebelPropertySet();
          for (int i = 0; i < jsonArray.size(); i++) {
            if ((jsonArray.get(i).isJsonObject()) || (jsonArray.get(i).isJsonArray()))
            {

              temp.setType("i");
              child.addChild(JsonObjectToPropertySet(jsonArray.get(i).getAsJsonObject(), temp));
            }
            else
            {
              child.setProperty("i", jsonArray.get(i).getAsString());
            }
          }
          ps.addChild(child);
        }
        else if (jsonelement.isJsonObject())
        {
          jsonObject = jsonelement.getAsJsonObject();
          SiebelPropertySet child = new SiebelPropertySet();
          child.setType(mapEntry.getKey().toString());
          ps.addChild(JsonObjectToPropertySet(jsonObject, child));
        }
        else
        {
          ps.setProperty(mapEntry.getKey().toString(), mapEntry.getValue().toString().replace("\"", ""));
        }
      }
    }
    return ps;
  }

  public JsonObject PropertySetToJsonObject(SiebelPropertySet ps, JsonObject jObj)
  {
    JsonObject siebJSON = new JsonObject();

    String propName = ps.getFirstProperty();
    while (propName != "")
    {
      String propVal = ps.getProperty(propName);
      siebJSON.addProperty(propName, propVal);
      propName = ps.getNextProperty();
    }
    JsonObject child = new JsonObject();
    for (int i = 0; i < ps.getChildCount(); i++)
    {

      child = PropertySetToJsonObject(ps.getChild(i), child);
      siebJSON.add(ps.getChild(i).getType(), child);
    }
    return siebJSON;
  }

  public static void main(String[] args)
  {
    SiebelPropertySet input = new SiebelPropertySet();
    SiebelPropertySet output = new SiebelPropertySet();

    input.setProperty("excelname", "jbs.xlsx");
    input.setProperty("sheetID", "1");
    input.setProperty("skip", "0");
    input.setValue("{status:true,returnCode:0,message:Registeruserissuccess!,user:{brandCode:T,mobile:13900000000,customerName:姓名,sourceChannel:05,dealerCode:000001,memberNo:000001,idType:IdentityCard,email:test@qq.com,idCode:230111111111111111}}");

    EAIJSONConverter jbs = new EAIJSONConverter();
    try
    {
      jbs.doInvokeMethod("PropSetToJSON", input, output);
    }
    catch (SiebelBusinessServiceException e)
    {
      e.printStackTrace();
    }
    System.out.print(output);
  }
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐